Integer的值范围-128~127

看到一道面试题,这个面试题是这样的。

public class Foo {

    public static void main(String[] args) {
                Integer a = 120,b = 160;
                Integer c = 120,d = 160;
                System.out.println(a==c);
                System.out.println(a.equals(c));
                System.out.println(b==d);
                System.out.println(b.equals(d));
    }
}

运行结果:

那么,会看到为什么 a==c 就是true, 而b==d 就是false了呢?

其实这样的,当我们给一个Integer赋予一个int类型的值的时候它会调用Integer的静态方法ValueOf()方法。

Integer a = Integer.valueOf(120);

Integer c = Integer.valueOf(120);

Integer b = Integer.valueOf(160);

Integer d = Integer.valueOf(160);

那这个valueOf()方法返回的integer是不是一个新的new Integer(120)?那这样的话它们应该为 == 为false,那么下面看下源码

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

这个源码中的方法,他会拿我们赋值的int值去判断是否存在缓存类的low和hign范围之间,如果我们int值在这个范围之间的话,取的是缓存类中的cache缓存数组中取值,否则的话就是new Integer(num);

那么这个缓存类integerCache是什么呢?看源码

   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() {}
    }

源码中有一个静态内部类,这个类定义了-128~127的范围,并且放到一个静态缓存数组cache中。在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中。

其实就一句话:

一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象。而不在此范围内的数值则要new到堆中了。

延伸:

public class Foo {

    public static void main(String[] args) {
                Integer in = new Integer(12);
                int t = 12;
                System.out.println(t == in);
    }
}

结果:

为什么int和integer比较是为true呢?看下反编译后的代码

Integer in = new Integer(12);
int t = 12;
System.out.println(t == in.intValue());




这个反编译后的代码,new Integer的进行了intValue()拆箱,拆箱后为int类型,int类型与int类型比较为true

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

油锅里的猪

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值