A brief analysis of the cache area of Integer (study notes)

First, let’s look at a question about Integer:
public class Test {
    
    
    public static void main(String[] args) {
    
    
        Integer a = 100;
        Integer b = 100;

        Integer c = 1000;
        Integer d = 1000;
        
        //引用类型"=="比较的是内存中的引用地址,不是比较数值的大小
        System.out.println(a==b);//true
        System.out.println(c==d);//false
    }
}


Why does the first one output true and the second output false?

When we execute Integer a = 100;, what is actually executed isInteger a = Integer.valueOf(100);

To investigate the reason, we need to look at the source code of Integer:

	public static Integer valueOf(int i) {
    
    
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

The valueOf method will first determine the numerical range of int i. Normally, IntegerCache.low=-128, IntegerCache.high=127


Then we can see a static code block from the inner class IntegerCache:

    private static class IntegerCache {
    
    
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
    
    
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
    
    
                try {
    
    
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
    
    
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {
    
    }
    }

It is not difficult to see that when the Integer class is loaded, all the numbers in [-128,127] will be saved in the Integer cache[] array, which is the cache area; and it is static and final, which also avoids repeated instantiation. and garbage collection.

When the Integer value is within the range of [-128,127], the corresponding Integer object will be obtained from the buffer array.

So as long as it is the same value, the object you get must point to the same cache area.


Back to the topic:

Because a and b are both 100, which is within the range of the cache area, you get the same Integer object, and use == to determine whether the result is true.

Both c and d are 1000, which does not fit within the cache area, so new Integer objects are generated respectively. Use == to judge, because they do not point to the same reference, and the final result is false.

Guess you like

Origin blog.csdn.net/liuzhuo13396/article/details/117597819