Introduction to the interconversion system of Android data types

  • 2020-05-05 11:51:58
  • OfStack

Some beginners to Android may have trouble converting JAVA data types, such as integer to float,double to double, integer to String to String, and processing and display time problems. The following author on the development of some experience to introduce to you.

As we know, the data types of android are divided into three categories: Boolean, character and numeric. Relative to the data type, the variable type of Java is Boolean boolean. Character char; Integer byte, short, int, long; Floating point float, double. Four integer variables and two floating point variables correspond to different precision and range respectively. In addition, we often use two kinds of variables, String and Date. Conversions between these variable types are often used in our programming, and in the following discussion, we will explain how to implement these conversions.

Type of data type conversion \r
There are three types of conversion for java data types:
(1). Conversion
between simple data types (2). Conversion of strings with other data types
(3). Other useful data type conversion
The three types of conversions are discussed below.

Conversion between simple data types
In Java, the integer, the real, and the character types are considered as simple data types, and these types are
from the low level to the high level [center](byte, short, char)--int--long--float--double[/center]
Conversions between simple data types can be broken down into:
● low-level to advanced automatic type conversion
● high to low level cast
● wrapper class transition types can be converted to

2.1 automatic type conversion
Low-level variables can be directly converted to high-level variables, which I call automatic type conversions. For example, the following statement can be passed directly in Java:
byte b;int i=b;long l=b;float f=b;double d=b;
If the low-level type is char, when converting to the high-level type (integer), the corresponding ASCII code value is converted, for example, \r
char c='c'; int i=c; System.out.println("output:"+i);
Output: output: 99;
For byte,short, and char types, which are parallel and therefore cannot be converted automatically to each other, the following casts can be used.
short i=99;char c=(char)i;System.out.println("output:"+c);
Output: output: c;
However, according to the author's experience, byte,short and int are all integers, so it is better to use int when manipulating integer data.

2.2 cast
It's a little more complicated when you're converting high-level variables to low-level variables, so you can use a cast. That is, you must use the following statement format:
int i=99;byte b=(byte)i;char c=(char)i;float f=(float)i;
As you can imagine, this conversion can definitely lead to overflow or loss of precision, so I do not recommend using this conversion.

2.3 wrap class transition type conversion
When we talk about conversions between other variable types, we need to know about Java's wrapper classes. The so-called wrapper classes can directly represent a variable of a simple type as a class, and we will use these wrapper classes a lot when performing conversions between variable types. Java has six wrapper classes: Boolean, Character, Integer, Long, Float, int, long, float, double. String and Date are classes themselves. So there is no notion of a wrapper class.
When converting between simple data types (automatic or cast), we can always use wrapper classes for intermediate transitions.
Typically, by declaring a variable and then generating a corresponding wrapper class, you can cast using the various methods of the wrapper class. For example:
Example 1, when you want to convert float to double:
float f1 f = 100.00; Float F1 = new float (f1); Double d1 = F1. doubleValue (); // F1.doubleValue () is the method
that returns the double value type of the Float class When you want to convert double to int:
double d1=100.00; Double D1=new Double(d1); int i1=D1.intValue();
When you want to convert int to double, automatically convert:
int i1=200; double d1=i1;
Variables of a simple type are converted to the corresponding wrapper class, and the constructor of the wrapper class can be used. Namely:
Boolean(boolean value), Character(char value), Integer(int value), Long(long value), Float(float value), Double(double value)
In each wrapper class, a method of ××Value() is always visible to get its corresponding simple type data. Using this method, it is also possible to convert between variables of different numeric types. For example, for a double precision real type class, intValue() can get its corresponding integer variable, while doubleValue() can get its corresponding double precision real type variable.

Related articles: