Java Integer why the comparison with == address the results will be different?

table of Contents


1. Problem

  • Integer why the comparison with == would address different result?
  • In the following example, why i1 == i2it is false, i3 == i4but it is true?
public class Test {
    public static void main(String[] args) {
        Integer i1 = 250;
        Integer i2 = 250;
        Integer i3 = 250;
        Integer i4 = 250;
        System.out.println(i1==i2);//false
        System.out.println(i1.equals(i2));//true
        System.out.println(i3==i4);//true
        System.out.println(i3.equals(i4));//true
    }
}

2. Answers

  • Integer class IntegerCache cache class exists, a default cache Integer value of -128 to 127, in case the value range [-128,127] can be converted directly from the IntegerCache when Integer;
  • Why do this cache [-128,127] it?
  • [-128,127] byte is the range, in the IO streams, each read document, the basic unit is a byte. 1, can be used IntegerCache cache class object does not create a large number of automatic packing time;
  • API reference as follows:
    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * jdk.internal.misc.VM class.
     *
     * WARNING: The cache is archived with CDS and reloaded from the shared
     * archive at runtime. The archived cache (Integer[]) and Integer objects
     * reside in the closed archive heap regions. Care should be taken when
     * changing the implementation and the cache array should not be assigned
     * with new Integer object(s) after initialization.
     */

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

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

            // Load IntegerCache.archivedCache from archive, if possible
            VM.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
        
        private IntegerCache() {}
    }
Published 201 original articles · won praise 200 · Views 6360

Guess you like

Origin blog.csdn.net/Regino/article/details/104733858