1. Elaboration of basic data types
The Java language provides eight basic types. Six numeric types (four integer types, two floating point types), one character type, and one Boolean type.
【Note】JAVA does not have an unsigned type
(1). Integer: int, short, byte, long
(2). Float type: float, double
(3). Character: char
(4). Boolean
Basic type size minimum value 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 class variables, namely String and Date.
2. Data conversion
1 Types of data type conversion There are generally three types of conversion of Java data types, namely:
(1). Conversion between basic data types (2). Conversion between strings and other data types (3). Conversion of other practical data types
2 Conversion between basic data types <br />The basic types from low to high are (byte, short, char)--int--long-float--double
The conversion between simple data types can be divided into:
●Low-level to advanced automatic type conversion ●Advanced to low-level cast type conversion ●Packaging class transition type can be converted
2.1 Automatic type conversion 2.1.1 Low-level variables can be directly converted to high-level variables, called automatic type conversion. For example, the following statements can be directly passed in Java:
byte b; int i=b; long l=b; float f=b; double d=b;
2.1.2 If the low-level type is char type, it will be converted to the corresponding ASCII code value when converting to a high-level type (integral type), for example
char c='c'; int i=c; System.out.println("output:"+i);Output: output:99;
2.1.3 For the three types of byte, short, and char, they are horizontal and therefore cannot be automatically converted to each other. The following cast type conversion is used.
short i=99 ; char c=(char)i; i =(short) c; System.out.println("output:"+c); Output: output:c;
But according to experience, byte, short, and int are all integer types, so if you operate integer data, it is best to use the int type uniformly.
2.2 When casting converts high-level variables to low-level variables, the situation will be more complicated, and you can use casting. That is, you must use the following statement format:
int i=99; byte b=(byte)i; char c=(char)i; float f=(float)i;
As you can imagine, this conversion may certainly lead to overflow or a decrease in accuracy.
2.3 Packaging class transition type conversion When we discuss the mutual conversion between other variable types, we need to understand Java's packaging class. The so-called packaging class means that we can directly represent simple type variables as a class. When performing mutual conversion of variable types, we will use these packaging classes a lot. Java has six packaging classes, namely Boolean, Character, Integer, Long, Float and Double. From the literal perspective, we can see that they correspond to boolean, char, int, long, float and double respectively. And String and Date are classes themselves. So there is no concept of packaging.
When converting between simple data types (automatic conversion or cast), we can always use the wrapper class for intermediate transitions.
Generally speaking, we first declare a variable and then generate a corresponding wrapper class, and we can use various methods of the wrapper class to perform type conversion. For example:
Example 1: When you want to convert the float type to a double type:
float f1=100.00f; Float F1=f1;//Autobox double d1=F1.doubleValue();//F1.doubleValue() is a method to return a double value type of the Float class. When you want to convert the double type to an int type: double d1=100.00; Double D1=new Double(d1);//Calling the constructor int i1=D1.intValue();
Convert variables of simple types to the corresponding packaging class, and can be directly assigned using the constructor and automatic packing of the packaging class. Right now:
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 method of ××Value() to obtain its corresponding simple type data. Using this method, conversion between different numerical variables can also be realized. For example, for a double-precision real-type class, intValue() can obtain its corresponding integer variable, and doubleValue() can obtain its corresponding double-precision real-type variable.
3 Conversion of string type and other data types <br /> By looking at the member methods provided by each class in the class library, you can see that almost all classes derived from the java.lang.Object class provide the toString() method, that is, converting the class into a string. For example: Characrer, Integer, Float, Double, Boolean, Short and other classes toString() method is used to convert characters, integers, floating point numbers, doubles, logical numbers, short integers and other classes into 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; //Autobox Double D1=d1; //Call the toString() method of the wrapper class separately to convert it to string 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); Sysytem.out.println("sd1"+sd1); 4. Convert the character type directly as a numeric value to other data types <br />For example, '1' refers to the value 1, rather than its ASCII code, for this conversion:
char c = '1'; //Character's getNumericValue(char ch) method int i = Character.getNumericValue(c); //ASCII code subtraction i = c -'0';
5. Conversion between Date class and other data types <br />There is no direct correspondence between integer and Date class. You can use the int type to represent year, month, day, hour, minute, and seconds respectively, so a correspondence is established between the two. When doing this conversion, you can use three forms of the Date class constructor:
Date(int year, int month, int date): Int type represents year, month, and day Date(int year, int month, int date, int hrs, int min): Int type represents year, month, day, hour, and minute Date(int year, int month, int date, int hrs, int min, int sec): Int type represents year, month, day, hour, minute, and second
There is an interesting correspondence between long integers and Date classes, which is to represent a time as the number of milliseconds from 0:00:00 GMT on January 1, 1970. For this correspondence, the Date class also has its corresponding constructor: Date(long date)
Get the year, month, day, hour, minute, second and week in the Date class. You can use the getYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(), getDay(), and you can also understand it as converting the Date class into an int.
The getTime() method of the Date class can get the long integer number corresponding to the time we mentioned earlier. Like the wrapper class, the Date class also has a toString() method that can convert it into the String class.
Sometimes we want to get a specific format of Date, for example 20020324, we can use the following method, first introduce it at the beginning of the file,
import java.text.SimpleDateFormat; import java.util.*; java.util.Date date = new java.util.Date(); //If you want to get the format of YYYYMMDD SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMDD"); String dateFormat=sy1.format(date); //If you want to 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);To view more Java syntax, you can follow: "Thinking in Java Chinese Manual", "JDK 1.7 Reference Manual Official English Version", "JDK 1.6 API java Chinese Reference Manual", "JDK 1.5 API java Chinese Reference Manual". I also hope that everyone will support Wulin.com more.