Take a look at the code first:
publicclass Main{ publicstaticvoidmain(String[] args){ 100; 100; 200; 200; ‘‘‘//输出结果‘‘‘ System.out.println(num1==num2); System.out.println(num3==num4); }}
Guess what the result is?
A lot of people will think the results are all true, but that's not the result.
truefalse
Why is this the result? If memory is used to interpret the results, NUM1 and num2 point to the same object, while Num3 and Num4 point to objects that are indeed different. Next, let's tell you why, take a look at the source code for the valueof method of the integer type:
publicstaticvalueOf(int i) { assert127; if (i >= IntegerCache.low && i <= IntegerCache.high) return128]; returnnew Integer(i); }
Where Integercache is implemented:
"''///Integercache, an inner class, notice that its properties are defined as static final '"' Private Static class integercache { Static Finalint high;"''//cache upper bound, temporarily null '"' Static FinalInteger cache[];"''//Cached integer array '"' "''//block, why define as block '"' Static{Finalint low =- -;"''//Cache nether, immutable. Only the upper bound can change '"' "''//high value is configured by property '"' "'The '//h value can be adjusted by setting the Autoboxcachemax parameter of the JDK (explained below), and the automatic buffer room is set to [ -128,n]. Note that the lower bound of the interval is fixed '"'int h =127;if(Integercachehighpropvalue! =NULL) {"''//use long.decode here to avoid invoking methods that '"' "''//require Integer 's autoboxing cache to be initialized"''//By decoding Integercachehighpropvalue, and getting a candidate upper bound value '"'int i = Long.decode (integercachehighpropvalue). Intvalue ();"''//take a larger upper bound, but not greater than the integer boundary max_value '"'i = Math.max (i,127);"''//Maximum array size is Integer.max_value '"'h = math.min (i, Integer.max_value--low); } high = h;"''//upper bound OK '"' "''//You can create a cache block, note the cache array size '"'Cache =Newinteger[(high-low) +1];//int j = Low; for(int k =0; K < Cache.length; k++) Cache[k] =NewInteger (j + +);"''//-128 to high value is assigned to the cache array individually '"'}PrivateIntegercache () {}}
As you can see from these two pieces of code, when you create an integer type object by using the ValueOf method, the value range is [-128,127], the value is in this interval, The pointer points to an object reference that already exists in the Integercache.cache, and when the value exceeds this range, a new object is created.
One thing to note is that not all types are this range, see Double type:
publicclass Main{ publicstaticvoidmain(String[] args){ 100.0; 100.0; 200.0; 200.0; System.out.println(i1==i2); System.out.println(i3==i4); }}
The result of the final output:
falsefalse
Specifically why the outcome of this, we can go to see the source code of the double valueof method implementation, and the integer valueof method is different, because the number of integer values in a range is limited, and the floating-point numbers are not.
Note that the implementations of the valueof methods of the classes Integer, short, Byte, Character, long are similar.
The implementation of the valueof method of Double and float is similar.
Pull down one, the result of a Boolean type has two true or False. Read the source code directly:
publicstaticvalueOf(boolean b) { return (b ? TRUE : FALSE); }
Where true and false are defined as:
public static final Boolean TRUE = new Boolean(true);‘‘‘/** ‘‘‘‘‘‘* The <code>Boolean</code> object corresponding to the primitive ‘‘‘‘‘‘* value <code>false</code>. ‘‘‘‘‘‘*/‘‘‘public static final Boolean FALSE = new Boolean(false);
Novice Java (v)----in-depth analysis of Java unboxing