原因:
因为在Integer.class文件中,有一个静态内部类IntegerCache;
会在系统加载时,将(low = -128到h = 127)之间的数据提前包装成Integer对象放入数组cache中;
Integer a = 111;
Integer c = 111;
System.out.println(a==c); 
valueOf()方法可以看出来,如果数值在(low = -128到h = 127)之间,会去IntegerCache.cache[]数组中查找并返回;
因为是同一个对象,所以使用 '= =' 判断肯定是true;如果不在(low = -128到h = 127),会创建一个新的Integer对象,用'= ='判断肯定就不等了;使用equals()对比,会将Integer先转为int值,再对比就相等了
注意:自动装箱使用的就是valueOf()实现的
@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;
}
注意:自动拆箱使用的是intValue()实现的
@IntrinsicCandidate
public int intValue() {
    return value;
}
举例:
自动拆箱:
int a = 1121;
Integer c = 1121;
System.out.println(a==c); // true 在比较时会将Integer的变量转为int类型比较,结果为true 

public class JavaPTest {
    public static void main(String[] args) {
        // -128  127
        int a = 1121;
        Integer c = 1121;
        System.out.println(a+c); // 
    }
}

自动装箱:
public class JavaPTest {
    public static void main(String[] args) {
        // -128  127
        Integer a = Integer.valueOf("1121");
        Integer c = 1121;
        System.out.println(a == c); // 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;
    }
}
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号