Discussing the mutual conversion between JAVA Data Types
Source: Internet
Author: User
Discuss the conversion between JAVA data types-Linux general technology-Linux programming and kernel information. The following is a detailed description. Source: javaresearch
Some beginners may encounter difficulties in converting data types in JAVA, such as conversions between integers, float, and double, and between integers and strings, and Handling and displaying time issues. Below I will introduce some experiences in development to you.
We know that Java's data types are divided into three categories: Boolean, numeric, and numeric. The numeric type is classified into integer and floating-point; compared with 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. Type of data type conversion
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
(Byte, short, char) -- int -- long -- float -- double
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:
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.
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:
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
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.
3 Conversion between string type and other data types
By checking the member methods provided by various classes in the class library, we can see that almost all classes derived from the java. lang. Object Class provide the toString () method, which converts the class to a string. For example, the toString () method toString () of Characrer, Integer, Float, Double, Boolean, Short, and other classes () this method is used to convert characters, integers, floating-point numbers, double-precision numbers, logical numbers, and short integers into strings. As follows:
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.
Sometimes we want to get a specific Date format, such as 20020324. We can use the following method to first introduce the Date in the file,
Import java. text. simpleDateFormat; import java. util. *; java. util. date date = new java. util. date (); // if you want to obtain the YYYYMMDD format SimpleDateFormat sy1 = new SimpleDateFormat ("yyyyMMDD"); String dateFormat = sy1.format (date); // if you want to obtain the year separately, month, day SimpleDateFormat sy = new SimpleDateFormat ("yyyy"); SimpleDateFormat sm = new SimpleDateFormat ("MM"); SimpleDateFormat sd = new SimpleDateFormat ("dd "); string syear = sy. format (date); String smon = sm. format (date); String sday = sd. format (date );
Conclusion:
Of course, the author's discussion is only a one-person view. If you want to learn more about JAVA data type conversion, I suggest you refer to the JAVA class library java. util. * below
Integer class
Boolean class
Character class
Float class
Double Type
String type
Date class
According to your own needs, refer to various member methods of different classes to convert data types.
You can refer to JAVA online class library or download a copy, to more flexible use of the member method between data types conversion, IP Address: http://java.sun.com/j2se/1.3/docs/api/index.html
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.