Integer和int为什么在-128-127之间比较会相等

发布时间 2023-07-18 11:11:11作者: 幂次方

原因:

因为在Integer.class文件中,有一个静态内部类IntegerCache;
会在系统加载时,将(low = -128到h = 127)之间的数据提前包装成Integer对象放入数组cache中;

int a = 111l;
Integer c = 111l;
System.out.println(a==c); // 在次会发生自动的装箱,将a转换成Integer在对比

字节码文件可以看出来,使用的是valueOf()方法将int转为了Integer对象;如果数值在(low = -128到h = 127)之间,会去IntegerCache.cache[]数组中查找并返回;
因为是同一个对象,所以使用判断肯定是true;如果不在(low = -128到h = 127),会创建一个新的Integer对象,用判断肯定就不等了;可以使用equals()对比,会将Integer先转为int值,再对比就相等了
image.png

@IntrinsicCandidate
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
    return value == ((Integer)obj).intValue();
}
return false;
}

同理

byte,short,int,long都是如此,区间默认都是(-128到127),Character是127
但是只有integer的最大范围是可以调整的;
配置java.lang.Integer.IntegerCache.high // 可以调整127的上限,不能大于int的最大值

源码:

IntegerCache源码:

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"); // 可以调整127的上限,不能大于int的最大值
        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
        CDS.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++); // 包装成Integer对象放入数组
            }
            archivedCache = c;
        }
        cache = archivedCache;
        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}

CharacterCache源码:

private static class CharacterCache {
    private CharacterCache(){}

    static final Character[] cache;
    static Character[] archivedCache;

    static {
        int size = 127 + 1;

        // Load and use the archived cache if it exists
        CDS.initializeFromArchive(CharacterCache.class);
        if (archivedCache == null || archivedCache.length != size) {
            Character[] c = new Character[size];
            for (int i = 0; i < size; i++) {
                c[i] = new Character((char) i);
            }
            archivedCache = c;
        }
        cache = archivedCache;
    }
}

LongCache源码:

private static class LongCache {
    private LongCache() {}

    static final Long[] cache;
    static Long[] archivedCache;

    static {
        int size = -(-128) + 127 + 1;

        // Load and use the archived cache if it exists
        CDS.initializeFromArchive(LongCache.class);
        if (archivedCache == null || archivedCache.length != size) {
            Long[] c = new Long[size];
            long value = -128;
            for(int i = 0; i < size; i++) {
                c[i] = new Long(value++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
    }
}

ByteCache源码:

private static class ByteCache {
    private ByteCache() {}

    static final Byte[] cache;
    static Byte[] archivedCache;

    static {
        final int size = -(-128) + 127 + 1;

        // Load and use the archived cache if it exists
        CDS.initializeFromArchive(ByteCache.class);
        if (archivedCache == null || archivedCache.length != size) {
            Byte[] c = new Byte[size];
            byte value = (byte)-128;
            for(int i = 0; i < size; i++) {
                c[i] = new Byte(value++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
    }
}

ShortCache源码

private static class ShortCache {
    private ShortCache() {}

    static final Short[] cache;
    static Short[] archivedCache;

    static {
        int size = -(-128) + 127 + 1;

        // Load and use the archived cache if it exists
        CDS.initializeFromArchive(ShortCache.class);
        if (archivedCache == null || archivedCache.length != size) {
            Short[] c = new Short[size];
            short value = -128;
            for(int i = 0; i < size; i++) {
                c[i] = new Short(value++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
    }
}