Conversion between Java variable types

Source: Internet
Author: User
Java data types are classified into three categories: Boolean, numeric, and numeric. The numeric type is classified into integer and floating point types. Compared with the data type, Java variable types are 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 ranges respectively. In addition, we often use two types of variables: string and date. Mutual Conversions between these variable types are often used in our programming. In our article today, we will look at how to implement these conversions in the future.

1. Mutual conversion among integer, real, and Complex Variables
In Java, integer, real, and struct types are treated as the same type of data. These types are respectively (byte, short, char) -- int -- long -- float -- double, low-level variables can be directly converted to advanced variables. For example, the following statement can be directly passed in Java:
Byte B;
Int I = B;
However, converting advanced variables to low-level variables is more complex. You can use forced type conversion. You must use the following statement format:
Int I;
Byte B = (byte) I;
As you can imagine, this kind of conversion will certainly lead to overflow or a decrease in precision, so we do not recommend this kind of conversion.

Ii. Java Packaging
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.

Iii. Mutual conversion between simple type variables and packaging classes
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.

Iv. Conversion Between the string class and other data types
For the preceding packaging classes, except character, there are constructors that can directly use string parameters, which makes it quite easy to convert the string class to these data types, that is:
Boolean (string S), INTEGER (string S), long (string S), float (string S), double (string S)
To convert the string class to the date class, you can also use this constructor: Date (string S)
Now we have another struct variable. In fact, the string class can be understood as a char array, so we can find this method in the string class to achieve this conversion: charat (INT index) the character at a position in the string class can be obtained. tochararray () can also convert the entire string class into a char array.
For all packaging classes, a method named tostring () can be converted to the corresponding string class. For integer and long integer classes, you can also use tobinarystring (int I) tohexstring (int I), tooctalstring (int I) are converted to the string class in the form of binary, hexadecimal, and octal, respectively.

5. Convert the numeric type directly to another data type.
There are actually two mappings between converting a numeric variable to a numeric variable. In the conversions we mentioned in the first part, we actually convert it into the corresponding ASCII code, however, we sometimes need another Conversion Relationship. For example, '1' refers to the value 1 rather than its ASCII code. For such conversion, we can use the getnumericvalue (char ch) method of character.

Vi. Conversion Between the date class and other data types
There is no direct correspondence between the integer type and the date type, but you can use the int type to indicate year, month, day, hour, minute, and second, respectively, in this way, a ing relationship is established between the two. In this conversion, you can use the date class constructor in three forms:
Date (INT year, int month, int date): indicates the year, month, and day in the int type.
Date (INT year, int month, int date, int hrs, int min): indicates the year, month, day, hour, and minute in the int type.
Date (INT year, int month, int date, int hrs, int min, int Sec): year, month, day, hour, minute, second
There is an interesting ing between long integer and date classes, that is, to express a time as the number of milliseconds from 00:00:00, January 1, January 1, 1970, GMT. For this correspondence, the date class also has its corresponding constructor: Date (long date)
You can use the getyear (), getmonth (), getdate (), gethours () the getminutes (), getseconds (), and getday () methods can also be interpreted as converting the date class to an int.
The gettime () method of the date class can obtain the long integer number corresponding to the time we mentioned earlier. Like the packaging class, the date class also has a tostring () the method can be converted to the string class.
Some other methods are available in data type conversion in Java. However, the methods described above are sufficient for your actual programming, aren't they?

I have also encountered encoding problems before. The solution is to put a layer of filter transcoding after the input, and put another filter in the output. In this way, when there are other migrations, directly change the filter code.

The filter code is as follows:

Public static string syconvertcode (string tempsql ){
If (tempsql = NULL)
Tempsql = "";
Else
Tempsql = tempsql. Trim ();
String returnstring = tempsql;
Try {
// Byte [] ASCII = returnstring. getbytes ("GBK ");
// Returnstring = new string (ASCII, "ISO-8859-1 ");
Byte [] ASCII = returnstring. getbytes ("ISO-8859-1 ");
Returnstring = new string (ASCII, "GBK ");

} Catch (exception e ){
E. printstacktrace ();
}
Return returnstring;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.