Let’s take a look at a piece of code:
public class Main{ public static void main(String[] args){ Integer num1 = 100; Integer num2 = 100; Integer num3 = 200; Integer num4 = 200; '''//Output result''' System.out.println(num1==num2); System.out.println(num3==num4); }} Guess what the result is?
Many people think that the result is true, but the result is not like this.
true
false
Why is this result? If the result is explained by memory, num1 and num2 point to the same object, while num3 and num4 point to different objects. Next, let’s tell you why, take a look at the source code of the valueof method of type Integer:
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; return new Integer(i); }The implementation of IntegerCache:
'''// IntegerCache, an internal class, note that its properties are defined as static final''' private static class IntegerCache { static final int high; '''// cache upper bound, temporarily null''' static final Integer cache[]; '''// cache integer array ''' ''// block, why is it defined as block ''' static { final int low = -128; '''// cache lower bound, immutable. Only the upper bound can change '''' '''// high value may be configured by property''' '''// h value, which can be adjusted by setting jdk's AutoBoxCacheMax parameter (there is an explanation below), and the automatic cache interval 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, a candidate upper bound value is obtained ''' int i = Long.decode(integerCacheHighPropValue).intValue(); '''// Take the larger one as the upper bound, but it cannot be greater than the boundary of Integer MAX_VALUE''' i = Math.max(i, 127); '''// Maximum array size is Integer.MAX_VALUE''' h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; '''//The upper bound is determined'''' '''// You can create a cache block, pay attention to the cache array size''' cache = new Integer[(high - low) + 1]; // int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); '''// -128 to high values are allocated to cache array one by one ''' } private IntegerCache() {} }Through these two pieces of code, it can be seen that when creating an Integer type object through the valueof method, the value range is [-128, 127]. In this interval, the pointer points to an object reference already existed in IntegerCache.cache. When the value exceeds this range, a new object will be created.
One thing to note is that not all types are in this range, look at the Double type:
public class Main{ public static void main(String[] args){ Double i1 = 100.0; Double i2 = 100.0; Double i3 = 200.0; Double i4 = 200.0; System.out.println(i1==i2); System.out.println(i3==i4); }}Final output result:
false
false
For the specific reason why this is the result, you can check the implementation of the Double valueof method in the source code. It is different from the Integer valueof method because the number of integer values in a certain range is limited, but floating point numbers are not.
Note that the implementation of valueOf methods of Integer, Short, Byte, Character, and Long are similar.
The implementation of the valueOf method of Double and Float is similar.
Pull down one, and the results of Boolean type have two True or False. Look directly at the source code:
public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }The TRUE and FALSE are defined as follows:
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);
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.