关于Integer的一道面试题

这是一道面试题

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub      
        Integer i1 = 100;       
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
        Integer i5 = 600;
        Integer i6 = 600;       
        System.out.println(i1==i2);
        System.out.println(i3==i4);
        System.out.println(i5.equals(i6));
    }
}

返回结果:

true
false

当我第一次看到时候,也会觉得是两个都应该是true

那么就看一下源码
(我这里是jdk1.8的源码),这里仅摘取关键部分源码。

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++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}

从这段代码中我们可以看到,Integer对象在其内部是有缓存机制的,它缓存区间的值在-128~127之间,存放在自定义的cache数组当中。
而对于我们的Integer a = 100来说,它实际在内部做了Integer a = Integer.valueOf(100)类似的操作,所以我们再来看下valueOf这个方法的具体操作,代码如下:

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

在IntegerCache静态内部类中,我们发现IntegerCache.low的值为-128,IntegerCache.high的值为127。
而valueOf(int i)这个方法很明显的意思就是说:

如果传入的值在区间-128~127之间,则从Integer包装类的静态内部类IntegerCache中的cache数组中取出对应的缓存对象。
如果传入的值不在区间-128~127之间,那么新建一个Integer包装对象。
换句话说,对于Integer var = ?这种在-128~127区间的赋值,Integer对象是在Integer.IntegerCache.cache中产生,所以可以复用已经存在的对象,也就是说在这个区间内的Integer值可以直接使用==来进行判断。而在这个区间之外的数该如何判断?在这个区间之外的数都会在堆上产生一个新的对象,从而不会复用已经存在的对象,所以这个时候==就不能够对其进行判断,而应该使用equals方法来判断。
因此,上面题目中的c == d结果为false就说得通了。

另外
java “==”就是比较引用,没有重载过
而由于-128至127内的Interger都是同一个对象引用,所以第一个比较式true
之外的Interger 则不是同一个对象,所以比较为false
重载的应该是equals()方法,所以如果用equals来比较都为true

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub      
        Integer i1 = 100;       
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;
        Integer i5 = 600;
        Integer i6 = 600;       
        System.out.println(i1==i2);
        System.out.println(i3==i4);
        System.out.println(i5.equals(i6));
    }
}

返回结果:

true
false
true

你可能感兴趣的:(关于Integer的一道面试题)