1 , basic type
In Java there are eight basic types, divided into four categories, namely:
Integer: include byte, short, int, long
Generics: float, double
Character: char
Boolean: boolean
2 , the length of the basic types and default values
3 , the type of packaging
4, unpacking boxes
Unpacking, i.e., converting the basic type based package type
Packing, i.e., type into a corresponding basic type of packaging.
Such as: Integer no = 1; // autoboxing
Integer no2 = new Integer (2); // packing
The actual operation of the automatic packing corresponding Integer.valueOf (1);
Automatically modifies the java files generated class file process is Integer.valueOf (1), it can be viewed by decompiling tool.
5 Notes
1) Do not compare the use of packaging types ==
As Integer no1 = 10; Integer no2 = 10; no1 == no2; returns a value of true
Integer no1 = 200; Integer no2 = 200; no1 == no2; return result is false
Byte, Short, Integer, Long, in the definition of Character has a when a caching mechanism, -128 to 127 corresponding to the object is cached to the cache, call valueOf () method first determines whether the data is within this range, if the the range, returns the cache object, if out of range, a new object is returned.
Therefore, the numerical values within this range, == Returns true comparison. Otherwise it will cause some intermittent bug, difficult to locate.
2) basic types java in length will not change, the number of bits may vary according to the editor, it does not change in the java c, c ++ and c # basic type length.
3)boolean官方没有规定长度,实际的占用空间长度和虚拟机有关系。
4)基本类型在虚拟机中是存储在栈中。