Java自动拆装箱之缓存机制

本文通过一个示例解释了Java中Integer自动装箱时的缓存机制,当数值在-128到127之间时,Integer对象会从缓存中获取,否则会创建新的对象。同样,Byte、Short、Character和Boolean也有类似的缓存机制,但Float和Double没有。
摘要由CSDN通过智能技术生成

题目: 输出下列结果

		Integer i1 = 127;
		Integer i2 = 127;
		System.out.println(i1 == i2);
		
		Integer i3 = 128;
		Integer i4 = 128;
		System.out.println(i3 == i4);

答案

             true

             false

 

第一感觉

              what???什么鬼???难道不应该是true和true吗?

 

解析

           Integer i1 = 127; 这条语句进行了自动装箱,调用Integer里面的valueOf()方法。而valueOf源码如下

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

            可以看到调用valueOf时先进行了判断,i是否在IntegerCache.low和IntegerCahe.high之间,如果在,则执行

               IntegerCache.cache[i + (-IntegerCache.low)]

              IntegerChache源码如下

   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;
        }
    ....// 省略

       从上面的源码我们可以看到IntegerCache是一个静态内部类,静态代码块中给我们初始化了cache这个数组,范围从-128到127。结合上面我们可以得到,Integer自动装箱的数在-128到127之间时是从这个常量数组中取值,这个常量数组我们也可以称为缓冲区。

      下面回到上面的代码

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

    当需要自动装箱的类不在IntegerCache.low和IntegerCache.high中时,不会到缓存区里面取值,返回的是新创建的对象

         两个引用类型数据进行==比较时,比较的是地址值,这也就是为什么i3 和 i4进行==比较时不相等的原因

    

    进一步猜想: 其他数据类型是否也存在缓存呢?

 

    对源码分析后得到下面结论

 

            Byte、Short、Integer、Long 这四个整数类型包装类存在缓存区,缓存范围为-128-127

            Character字符类型存在缓存区,缓存范围为0-127

            Boolean布尔类型包装类存在缓存区,缓存范围为TRUE和FALSE

             Float、Double这两个浮点数类型包装类没有缓存区,直接返回new Float(x)和new Double(x)

      

 

       

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值