Integer类型的数值比较案例分析

Integerint的的包装类,在Java5之后,可以进行自动装箱和自动拆箱,也就是Integerint可以进行转换本文以一个Integer类型的数值比较的例题,分析其中一个容易错的点

问题:下面代码中输入的结果为?

package Test01;

/**
 * @ClassName IntegerTest
 * @Author 长歌
 * @Date 2020/5/19 0:19
 * @Version 1.0
 */
public class IntegerTest {
    public static void main(String[] args) {
		//定义4个Integer的对象引用
        Integer a = 10, b = 10, c = 200, d = 200;
		//引用内容值的比较
        System.out.println(a.equals(b));
        System.out.println(c.equals(d));
		//引用的地址的比较
        System.out.println(a == b);
        System.out.println(c == d);
    }
}

在上面代码中,一眼看过去,可能会觉得结果是两个true和两个false,前面两个比较的是引用内容的值,毫无疑问是true
那么后两个呢?比较的是引用对象的地址,所以都是false吗?

我们都知道上面的 a、b、c、d四个变量都是Integer的对象引用,所有“==”比较的是引用的地址而不是值,按理说,后面两个都是false,但事实上结果是:

可以看出来,除了System.out.println(a == b);的结果出乎意料的是true,其他都是如前面分析一样

那么为什么 a == b为什么为true呢?
我们来看下其中发生了什么,首先,当我们给Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf()valueOf源码如下:

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

IntegerCache.low<=i <=IntegerCache.high时,执行IntegerCache类里面的方法,否则返回new Integer(i),而IntegerCacheInteger的内部类,源码如下:

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) {
                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);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

概括起来就是,如果int的值在 -128 ~ 127 之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,即引用同一对象,所以 a == btruec == dfalse

posted @ 2021-10-08 14:10  长歌→  阅读(46)  评论(0编辑  收藏  举报