Some new Android users may be troubled by the conversion between JAVA data types, such as the conversion between integer, float, and double types, and between integer and String types, and Handling and displaying time issues. Below I will introduce some experiences in development to you.
We know that the data types of android are divided into three categories: Boolean, numeric, and numeric, while the numeric type is classified into integer and floating point; relative to the data type, java's variable types include boolean, char, byte, short, int, and long; float and double. Four Integer Variables and two floating point variables correspond to different precision and ranges respectively. In addition, we often use two types of variables: String and Date. The conversions between these variable types are often used in programming. In the following discussion, we will explain how to implement these conversions.
1 Data Type Conversion Type \ r
Java data type conversion is generally divided into three types:
(1) Conversion between simple data types
(2) Conversion between strings and other data types
(3) Conversion of other practical data types
Next we will discuss the three types of conversions respectively.
2 conversion between simple data types
In Java, integer, real, and struct types are considered as simple data types. These types are
[Center] (byte, short, char) -- int -- long -- float -- double [/center]
Conversions between simple data types can be divided:
● Low-level to advanced automatic type conversion
● Forced type conversion from advanced to low-level
● Conversion of packaging transition types
2.1 automatic type conversion
Low-level variables can be directly converted to advanced variables, which I call automatic type conversion. For example, the following statement can be directly passed in Java:
Byte B; int I = B; long l = B; float f = B; double d = B;
If the low-level type is char type, the conversion to the advanced type (integer) is converted to the corresponding ASCII code value, for example, \ r
Char c = 'C'; int I = c; System. out. println ("output:" + I );
Output: 99;
For the byte, short, and char Types, they are of the same level, so they cannot be automatically converted to each other. The following mandatory type conversion can be used.
Short I = 99; char c = (char) I; System. out. println ("output:" + c );
Output: c;
However, based on my experience, the three types of byte, short, and int are all integer. Therefore, we recommend that you use the int type when operating integer data.
2.2 forced type conversion
Converting advanced variables to low-level variables is complicated. You can use forced type conversion. 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 kind of conversion will certainly lead to overflow or reduced precision. Therefore, I do not recommend this kind of conversion.
2.3 conversion of packaging type transition
When we discuss the conversion between other variable types, we need to understand the Java packaging class. The so-called packaging class can directly represent a simple type variable as a class, when performing conversions between variable types, we will use these packaging classes in large quantities. Java has six packaging classes: Boolean, Character, Integer, Long, Float, and Double, literally, we can see that they correspond to boolean, char, int, long, float, and double respectively. String and Date are classes. So there is no concept of packaging.
When converting between simple data types (automatic or forced conversion), we can always use the packaging class for intermediate transition.
In general, we declare a variable first, and then generate a corresponding packaging class, we can use various methods of the packaging class for type conversion. For example:
Example 1: If you want to convert the float type to the double type:
Float f1 = 100.00f; Float F1 = new float (f1); Double d1 = F1.doubleValue (); // method that returns the double value type for a Float class whose F1.doubleValue () is
When you want to convert the double type to the int type:
Double d1 = 100.00; Double D1 = new Double (d1); int i1 = D1.intValue ();
If you want to convert the int type to the double type, automatic conversion is performed:
Int i1 = 200; double d1 = i1;
You can use the constructor to convert a simple type variable to a corresponding packaging class. That is:
Boolean (boolean value), Character (char value), Integer (int value), Long (long value), Float (float value), Double (double value)
In each packaging class, there is always a tangible method of ×× Value () to get the corresponding simple type data. This method can also be used to convert different numeric variables. For example, for a double-precision real-type class, intValue () can get the corresponding integer variable, while doubleValue () you can get the corresponding double-precision real variables.