Java中采用了装箱机制,并且自jdk1.5开始加入了自动装箱自动拆箱的特性,即基本数据类型和对应的引用数据类型可以直接进行赋值操作,这一定程度上简化了代码的编写,但是这其中也存在比较容易忽略的坑。这里记录一个面试中经常问到的坑:

@Test
public void test() {
    Integer i = new Integer(1);
    Integer j = new Integer(1);
    System.out.println(i == j);

    Integer m = 1;
    Integer n = 1;
    System.out.println(m == n);

    Integer x = 128;
    Integer y = 128;
    System.out.println(x == y);
}

运行结果:

false
true
false

运行结果的后两行就是坑的所在,第一个结果可以解释为:使用==运算符比较时,如果二者是引用类型,在没有重写equals()方法的前提下,比较的是二者的地址,ij由于使用new关键字创建,代表了两个不同的对象,所以输出false。之后两个的结果就有点诡异了,同为int类型装箱,但是拿来比较得到了完全不同的结果。

通过查看包装类的源码,我们发现在Integer类型中定义了一个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");
        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() {}
}
在类中定义了一个Integer类型的数组,保存了-128~127的整数,如果我们使用了自动装箱的方式,给Integer类型变量在这个范围内时,可以直接使用数组中的元素,而不再重新new一个对象,当数值类型超出这个范围时,就会重新new一个对象。因此在前面的例子中会出现截然相反的结果。这么做的目的是为了提高自动装箱时的运行效率。
最后修改:2021 年 03 月 08 日
如果觉得我的文章对你有用,请随意赞赏