Java data type conversion detailed _java

Source: Internet
Author: User
Tags wrapper

First, the basic data type elaboration

The Java language provides eight basic types. Six numeric types (four integers, two floating-point types), one character type, and one Boolean type.
"Note" Java has no unsigned type
(1). Integer: Int,short,byte,long
(2). Floating point type: float,double
(3). Character: Char
(4). Boolean: Boolean

Basic type size minimum maximum value
void
Boolean----------------
Char 16-bit Unicode 0 Unicode 2^16-1
BYTE 8-bit-128 +127
Short 16-bit-2^15 +2^15-1
int 32-bit-2^31 +2^31-1
Long 64-bit-2^63 +2^63-1
Float 32-bit IEEE754 IEEE754
Double 64-bit IEEE754 IEEE754

In addition, we often use two kinds of variables, string and date.

Second, data conversion

1 Types of data type conversions
The conversion of Java data types is generally divided into three categories, respectively:
(1). Conversion between basic data types
(2). Conversion of strings to other data types
(3). Other practical data type conversions

2 conversion between basic data types
Basic types are from low-level to advanced (Byte,short,char)--int--long--float--double
The conversion between simple data types can be divided into:
Low-level to advanced automatic type conversions
Advanced to low-level forced-type conversions
Wrapper class transition type can be converted

2.1 Automatic type Conversion
2.1.1 Low-level variables can be directly converted to advanced variables, called automatic type conversions, for example, the following statements can be passed directly in Java:

byte B; 
int i=b; 
Long l=b; 
float f=b; 

2.1.2 If the low-level type is char, the conversion to the Advanced Type (integer) is converted to the corresponding ASCII code value, for example

Char c= ' C '; 
int i=c; 

Output: output:99;

2.1.3 for Byte,short,char three types, they are peers, so they cannot be automatically converted to each other, using the following coercion type conversions.

Short i=99; 
Char c= (char) i; 
i = (short) C; 

Output: OUTPUT:C;
However, according to experience, byte,short,int three types are integral, so if you are working with integer data, it is best to use the int type uniformly.

2.2 Coercion Type conversion
When you convert an advanced variable to a low-level variable, the situation is a bit more complicated, and you can use coercion type conversions. That is, you must use the following statement format:

int i=99; 
byte b= (byte) i; 
Char c= (char) i; 

It can be imagined that this conversion is certainly likely to result in a drop in overflow or precision.

2.3 Wrapper class Transition type conversion
As we discuss the conversion of other variable types, we need to understand the Java wrapper class, the so-called wrapper class, is that you can directly represent a simple type of variable as a class, in the execution of variable types of mutual conversion, we will use the bulk of these wrapper classes. Java has a total of six wrapper classes, Boolean, Character, Integer, long, float, and double, literally we can see that they correspond to Boolean, char, int, long, float and double. and string and date themselves are classes. So there is no concept of packaging class.

When converting between simple data types (automatic or cast), we can always use the wrapper class for intermediate transitions.
In general, we first declare a variable, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for type conversion. For example:
Example 1, when you want to convert a float to a double type:

float f1=100.00f; 
Float f1=f1;//Automatic boxing 
double d1=f1.doublevalue ();//f1.doublevalue () is a method  for returning a double value of float class When you want to convert the double to int: 

double d1=100.00; 
Double D1=new double (D1);//Call Constructor 
int I1=d1.intvalue (); 

A variable of a simple type is converted to the appropriate wrapper class, and can be directly assigned using the constructor of the wrapper class and automatic boxing. That
Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value
and in each wrapper class, the total visible as Xxvalue () method, to get its corresponding simple type of data. By using this method, we can also realize the transformation between different numerical variables, for example, for a double-precision real class, Intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double precision real variable.

3 Conversion of string type to other data types
By looking at the member methods provided by the classes in the class library, you can see that all classes derived almost from the Java.lang.Object class provide the ToString () method that converts the class to a string. For example, the ToString () method ToString () method for classes such as Characrer,integer,float,double,boolean,short is used to convert classes such as characters, integers, floating-point numbers, doubles, logical numbers, and short integers to strings. As shown below:

int i1=10; 
float f1=3.14f; 
Double d1=3.1415926; 
 
Integer i1=new integer (i1);//Generate Integer class 
Float f1=f1;//Automatic boxing 
Double d1=d1; 
 
The ToString () method that invokes the wrapper class is converted to string 
SI1 = I1 + "";//This method is more general 
string sf1=f1.tostring (); 
String sd1=d1.tostring (); 
 
Sysytem.out.println ("SI1" +SI1); 
Sysytem.out.println ("SF1" +SF1); 

4. Convert the character type directly into a numeric value to another data type
For example, ' 1 ' means the value 1, not its ASCII code, for this conversion:

char c = ' 1 '; 
Character's Getnumericvalue (char ch) method 
int i = Character.getnumericvalue (c); 
ASCII code subtraction 

5. Date class and other data types convert to each other
There is no direct correspondence between the integral type and the date class. You can just use the int type to represent the year, month, day, time, minute, and second, so that you have a corresponding relationship between the two, and you can use the three forms of the date class constructor when making this transition:

Date (int year, int month, int date): For years, months, and days in int
Date (int year, int month, int date, int hrs, int min): type int to represent years, months, days, hours, minutes
Date (int year, int month, int date, int hrs, int min, int sec): For years, months, days, times, minutes, seconds, int

An interesting correspondence between the long and the date classes is to represent a time as the number of milliseconds from 0:0 Greenwich Mean Time of January 1, 1970 to 0 seconds. For this correspondence, the date class also has its corresponding constructor: date (long date)
Gets the year, month, day, time, minute, second, and week in the date class you can use the Date class getyear (), getmonth (), GetDate (), getHours (), getminutes (), getseconds (), Getday ( method, you can also interpret it as converting the date class to int.

The GetTime () method of the date class can get the long integer corresponding to the time we mentioned earlier, and like the wrapper class, the date class also has a ToString () method that can convert it to a string class.
Sometimes we want to get a specific format for date, such as 20020324, and we can use the following methods, starting with the introduction of the file,

Import Java.text.SimpleDateFormat; 
Import java.util.*; 
Java.util.Date Date = new Java.util.Date (); 
 
If you want to get the YyyyMMDD format 
simpledateformat sy1=new simpledateformat ("YyyyMMDD"); 
String Dateformat=sy1.format (date); 
 
If want to separate get year, 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); 

See more Java syntax, you can pay attention to: "Thinking in the Java Chinese manual", "JDK 1.7 reference manual in English," "JDK 1.6 API Java Chinese Reference manual", "JDK 1.5 API Java Chinese Reference manual", also hope that everyone Support the cloud-dwelling community.

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.