深度解析“128陷阱”

128陷阱指的是,两个Integer数据类型在一定范围内用==判断,会返回true。这个默默人范围是(-128~127)

默认范围是(-128~127)的结果,原因是:Integer在初始化的时候有一个cache数组,这个数组默认范围是-128~127之间。在这个范围内,Integer变量指向的是这个数组里面的一个元素。所以用==判断时,返回的结果为true(判断的是同一个对象的地址)。

    public static void main(String[] args) {
        Integer a1=-128;
        Integer a2=-128;
        Integer a3=127;
        Integer a4=127;
        Integer a5=128;
        Integer a6=128;
        System.out.println(a1==a2);//true
        System.out.println(a3==a4);//true
        System.out.println(a5==a6);//false
        Integer a7=new Integer(127);
        Integer a8=new Integer(127);
        System.out.println(a7==a8);//false
    }

如上图所示,Integer数据类型的值在(-128~127)之间,用==比较返回true;超出这个范围返回false。如果new一个Integer对象的话,那么比较的是两个不同的对象,返回false(new出来的对象,不存在“128陷阱”)

源码:

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

初始化cache数组的源码:

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
            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++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

除了Integer外,其他包装类也有缓存技术

基本类型装箱类型是否缓存缓存范围取值范围
byteByte-128~127-128~127
shortShort-128~127-2^15~(2^15-1)
intInteger-128~127-2^31~(2^31-1)
longLong-128~127-2^63~(2^63-1)
floatFloat
doubleDouble
charBoolean\u0000~\u0071\u0000~\uffff
booleanCharactertrue,falsetrue,false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值