Integer类的缓存机制

Integer的缓存

先看一道面试题

  • 考察缓存
//情景1
Integer a = 1;
Integer b = 1;
System.out.println(a == b);//true。b.intValue()

//情景2
Integer c = 128;
Integer d = 128;
System.out.println(c == d);//false

//情景3
Integer e = new Integer(1);
Integer f = new Integer(1);
System.out.println(e == f);//false
  • 考察拆箱
public class errorTest {
    public static void main(String[] args) {
        int a = 1;
        Integer b = Integer.valueOf(1);
        Integer c = new Integer(1);
        Integer d = new Integer(1);
        System.out.println(a == b);//true
        System.out.println(a == c);//true
        System.out.println(b == c);//false
        System.out.println(c == d);// false

    }
}

Integer源码

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;	
        //当前值在缓存数组区间段,则直接返回该缓存值
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        //否则创建新的Integer实例
        return new Integer(i);
    }
    
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        //IntegerCache初始化时,缓存数值为-128-127的Integer实例(默认是从-128到127)。
        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() {}
    }

该类的作用是将数值等于-128-127(默认)区间的Integer实例缓存到cache数组中。通过valueOf()方法很明显发现,当再次创建值在-128-127区间的Integer实例时,会复用缓存中的实例,也就是直接指向缓存中的Integer实例。

(注意:这里的创建不包括用new创建,new创建对象不会复用缓存实例,通过情景3的运行结果就可以得知)

二、其他的缓存类

不仅仅Integer具有缓存机制,Byte、Short、Long、Character都具有缓存机制。

private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }
  • ByteCache用于缓存Byte对象;
  • ShortCache用于缓存Short对象;
  • LongCache用于缓存Long对象;
  • CharacterCache用于缓存Character对象;

这些类都有缓存的范围,其中

Byte,Short,Integer,Long为 -128 到 127,

Character范围为 0 到 127。

除了 Integer 可以通过参数改变范围外,其它的都不行,Integer的缓存上界high可以通过jvm参数-XX:AutoBoxCacheMax=size指定,取指定值与127的最大值并且不超过Integer表示范围,而下界low不能指定,只能为-128。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值