Eight basic types:
- Boolean/1
- BYTE/8 -2^7~2^7-1
- CHAR/16 0~2^16-1
- Short/16-2^15~2^15-1
- Int/32-2^31~2^31-1
- Float/32
- Long/64-2^63~2^63-1
- Double/64
Each base type has a corresponding wrapper type, and the assignment between the base type and its corresponding wrapper type is done using automatic boxing and unpacking .
Integer x = 2; // Boxing Int-->integer int y = x; // Unpacking Integer-->int
The difference between new Integer (num) and integer.valueof (NUM) is that new integer (num) creates an object each time, and integer.valueof (num) may use the cached object. Causes multiple uses of integer.valueof (NUM) to obtain a reference to the same object. As shown in the following code:
Integer A =NewInteger (127); Integer b=NewInteger (127); Integer C= 127; Integer D= 127; Integer e= 128; Integer g= 128; System.out.println (A= = b);//false different objects, of course different addressesSystem.out.println (A = = c);//false different storage places, a constant pool, a heap, of course, different addressesSystem.out.println (c = = d);//true is in a constant pool, and 127 is between -128~127, so referencing an existing object without newSystem.out.println (E = = g);//false 128 is not between -128~127 and is equivalent to a new two integer object in the heap
Refer to valueof () source code: First determine whether the value is in the cache pool, if so, directly use the contents of the cache pool.
Public Static Integer valueOf (int i) { if (i >= integercache.low && i <= integercache . High) return integercache.cache[i + (-integercache.low)]; return New Integer (i);}
The integer cache pool size is set to -128~127
Static Final intLow =-128;Static Final intHigh ;Static FinalInteger cache[];Static { //High value is configured by property intH = 127; String Integercachehighpropvalue=Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if(Integercachehighpropvalue! =NULL) { Try { inti =parseint (Integercachehighpropvalue); I= Math.max (i, 127); //Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value-(-low)-1); } Catch(NumberFormatException nfe) {//If The property cannot is parsed into an int, ignore it.}} High=h; Cache=Newinteger[(high-low) + 1]; intj =Low ; for(intk = 0; K < Cache.length; k++) Cache[k]=NewInteger (j + +); //range [ -128, 127] must be interned (JLS7 5.1.7) assertIntegercache.high >= 127;}
Therefore, when the NUM value is between -127~128, the Integer object is created to see if there is a num value in the cache pool, and if it is not created in the cache pool, it is used directly; if NUM is not between -127~128, ValueOf uses the new integer (num ) method to create a new object in the heap.
Java also places the values of some other primitive types in the buffer pool, including the following:
- Boolean values True and False
- All byte values
- Short values between-128 and 127
- int values between-128 and 127
- Char in the range \u0000 to \u007f
Therefore, the objects in the buffer pool can be used directly when using the wrapper types corresponding to these basic types.
Java Basics-Basic types and operations