Mutual conversion between Java variable types

zhaozj2021-02-16  118

The mutual conversion of the Java variable type PineApple repost (participating: 33, expert score: 180) Published: 2002-11-7 12:25 PM: 1.0 Read: 8132 times

[Analysis: everywhere Add Time: 2001-12-16 13:44:04] String dd = String.valueOf (a); integrity converted to character Integer.toString (); Integer.parseInt (request.getParameter ( "userid" ); // string into an integer variable source: www.linuxaid.com.cn author: everywhere (yanshuaiyu@yahoo.com) we know, Java data types are divided into three categories, namely Boolean, character and Value type, and the numerical type is divided into integer and floating-point; relative to data type, Java variable type is Boolele, Boolean; character type char; integer, longte, short, int, long; floating point Float, Double. The four integer variables and the two floating-point variables correspond to different accuracy and scope. In addition, we often use two variables, both string and date. Mutual conversion between these variable types Our programming is often used. In our article today, we will see how to achieve these conversions. First, integer, real-purpose, characters variables in Java integer, real shape, Character type is considered as the same type of data, these types are low-level to advanced (Byte, Short, Char) - Int - long - float - double, low variables can be directly converted to advanced variables, for example below The statement can pass directly in Java: BYTE B; INT i = B; when converting advanced variables into low variables, the situation is complex, you can use forced type conversion. You must use the following statement format: INT i Byte b = (byte) i; I can imagine that this conversion may definitely lead to overflow or accuracy, so we don't recommend using this conversion. Second, Java's packaging class is in our discussion between other variable types When transitioning each other, we need to understand the Java packaging class, so-called packaging class, you can directly expand the simple type variable as a class. When performing the mutual conversion of variable types, we will use these packaging classes. Java has six Packaging class, is Boolean, Character, Integer, Long, Float, and Double, we can see them from the literal, respectively correspond to Boolean, CHAR, INT, Long, Float and Double. String and Date itself are classes. So there is no concept of packaging classes. Third, simple type variable The mutual conversion of the package class is 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.

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

New Post(0)