Mutual conversion in integer real number variables
In Java, integer real character types are regarded as the same data. These types are (byte short characters) from low to high. Low-level variables can be directly converted into high-level variables. For example, the following statement can be passed directly in Java.
Byte b; int I = b;
When converting high-level variables into low-level variables, the situation is more complicated. You can use cast, that is, you must use the following statement format.
int I; Byte b= (byte) i;
As you can imagine, this conversion will definitely lead to overflow or decrease in accuracy, so we do not recommend this conversion.
Second, Java packaging classes
When we discuss the conversion between other variable types, we need to understand the wrapper classes of Java. The so-called wrapper class can directly represent simple type variables as a class. When performing the conversion between variable types, we will use these wrapper classes Java*** a lot. There are six wrapper classes, namely Boolean character integer long floating point type and double precision type. Literally, we can see that they correspond to boolean char int long float and Double respectively, while String and Date are classes themselves, so there is no concept of wrapper class.
Transformation between three simple type variables and wrapper classes
By using the constructor of the wrapper class, you can convert a simple type variable into the corresponding wrapper class, that is,
Boolean (Boolean value) char (character value) Integer(int value) Long (long value) Float (floating point value) Double (double precision value)
In each encapsulated class, you can use the method of ××Value () to obtain its corresponding simple type data. This method can also be used to realize the conversion between different numerical variables. For example, for a double-precision real number class, intValue () can get its corresponding integer variable and doubleValue () can get its corresponding double-precision real number variable.
Conversion between four string classes and other data types
For the above wrapper classes, in addition to Character, there are constructors that can directly use string parameters, which makes it quite simple for us to convert string classes into these data types.
Boolean (string s) Integer (string s) Long (string s) Float (string s) Double (string s)
The constructor Date (Strings) can also be used to convert a string class into a Date class.
Now we have a character variable. In fact, the String class can be understood as a char array, so we can find such a method to realize this transformation in the String class. CharAt(int index) can get the character toCharArray () in a certain position in the string class, and the whole string class can be transformed into a char array.
All wrapper classes have a method called toString (), which can be converted into the corresponding string class. For integer classes and long integer classes, tobinaryString (int I) to hexstring (inti) tootalstring (inti) can be converted into binary hexadecimal and octal string classes respectively.
5. Convert the character type directly as a numeric value to other data types.
There are actually two corresponding relationships when converting a character variable into a numerical variable. In the conversion mentioned in the first part, we actually convert it into the corresponding ASCII code, but sometimes we need another conversion relationship, for example, the numerical value instead of its ASCII code. For this transformation, we can use the method of character's getNumericValue(char ch).
Conversion between six date classes and other data types
There is no direct correspondence between integer and Date classes, but you can use int types to represent year, month, day, hour, minute and second respectively, thus establishing the correspondence between them. There are three forms of date class constructors that can be used for this conversion.
Date(int year int month int date) means year, month, day and day (int year int month int date int hrs int min) means year, month, day, hour and minute (int year int month int date int hrs int min int sec) means year, month, day, hour and minute, and it is of type int.
There is an interesting correspondence between long integers and Date classes, that is, expressing a time as milliseconds from GMT, and for this correspondence, Date classes also have their corresponding constructor Date (long date).
Gets the year, month, day, hour, second and week in the date class. You can use the getyear () getmonth () getDate () gethours () getminutes () getseconds () getday () method of the date class. It can also be understood as converting the date class to int.
The getTime () method of the Date class can get a long integer, which corresponds to the time we mentioned earlier. Like the wrapper class, the Date class also has a toString () method, which can be converted into a String class.
Lishi Xinzhi/Article/program/Java/hx/20 13 1 1/27245