Java's simple type packaging

xiaoxiao2021-03-06  43

We know that Java's data types are divided into three categories, namely Boolean, character types, and numerical types, which are divided into integer and floating point; relative to data type, Java variable type is Boolean Boolean Character type char; integer, short, int, long; floating point float, double. Four of four integer variables and two floating-point variables correspond to different precision and ranges, respectively. In addition, we often use two types of variables, names, and DATE. Mutual conversion between these variable types is often used in our programming. In our article today, we will see how to implement these conversions. First, integer, real, character, characteristic variables are intellectual, real, and character types are treated as the same class, these types are low-level to advanced, respectively (Byte, Short, Char) - Int - long - float - double, low variables can be directly converted to advanced variables, for example, the following statements can pass directly in Java: byte b; int i = B; while converting advanced variables into low variables, The situation is complex, you can use forced type conversion. That is, you have to use the following statement format: INT i; byte b = (byte) i; you can imagine that this conversion may definitely lead to overflow or accuracy, so we do not recommend using this conversion. Second, Java packaging class When we discuss mutual conversion between other variable types, we need to understand Java packaging classes, so-called packaging classes, you can directly represent the simple type variable as a class, execute variable types When transitioning each other, we will use these packaging classes. Java has six packaging classes, namely Boolean, Character, Integer, Long, Float and Double, which we can see them from the literal, which correspond to Boolean, CHAR, INT, LONG, FLOAT, and DOUBLE. String and Date itself are class. So there is no concept of packaging classes. Third, the simple type variable and the mutual translation of the package class are converted to the corresponding packaging class, which can utilize the constructor of the package class. That is: Boolean (Boolean Value), Character (CHAR Value), Integer (Float Value), Double (Double Value), in each packaging class, total tangible × × Value () Method to get its corresponding simple type data. With this method, the conversion between different numerical variables can be achieved, for example, for a double-precision real type, intValue () can obtain its corresponding integer variable, while DoubleValue () can get its corresponding double precision Type variable.

Fourth, String class and other data types of mutual conversion For these packaging classes above, in addition to Character, there are structures that can directly use string parameters, which makes it converted to these data types to these data types. It is simple, namely: Boolean (String S), Integer (String s), Double (String S), converts the String class into the DATE class or use such constructor: Date (String S) Now we have a characteristic variable, in fact, the String class can be understood as a CHAR type array, so we can find such a method in the String class to implement this conversion: charat (int index) Get the character in a location in a String class, TochararRay () can convert the entire String class into an array of char. For all packaging classes, there is a method called toString () to convert it into a corresponding String class, and for integer and long integrings, you can also use TobinaryString (Int i), tohexString (INT i) TOOCTALSTRING (INT i) is converted to the String class in binary, hexadecimal and octal form, respectively. 5. Switching the characters directly as a numerical value to other data types to convert the character variable to the numerical variable. In fact, there are two corresponding relationships. In the conversion of our first part, it is actually converted. To the corresponding ASCII code, but we sometimes need another conversion relationship, for example, '1' refers to the value 1, not its ASCII code, for this conversion, we can use the Character GetNumericValue (CHAR CH) method . 6. There is no direct correspondence between the DATE and other data types and the DATE class, but you can use the int type to represent the year, month, day, time, minute, second, so There is a correspondence between the two. When doing this conversion, you can use the three forms of the Date class constructor: Date (int year, int month, int Date): year, month, day Date, Int HRS, INT MIN: Take the INT type, month, day, time, minute Date, Int Year, Int Month, Int Date, Int HRS, INT min, Int Sec Sec Sec Sec Sec Sec Sec Sec Sec Sec S : In the INT type, there is a very interesting correspondence between long intersurgery and DATE, time, minute, and seconds in the INT type, which is to expose a time to January 1, 1970. 0:0:05 seconds in milliseconds. For this correspondence, the Date class also has its corresponding constructor: Date (long Date) Getting the year, month, day, time, minute, seconds and weeks you can use the Date class getYear (), getMonth ( ), GetDate (), getHours (), getminutes (), getseconds (), getDay () method, you can also understand it to convert the DATE class into int. The DATE class's getTime () method can get a long-intensive number corresponding to the previous time, like the package class, and the Date class also has a toString () method can convert it to the String class. In Java's data type conversion, you still have other methods available, but these methods described above are enough for your actual programming, isn't it?

转载请注明原文地址:https://www.9cbs.com/read-75062.html

New Post(0)