Integer缓存池知道吗?

文章介绍了Java中Integer和Long类型的缓存池机制,当值在-128到127范围内时,使用缓存提高性能。IntegerCache可通过JVM参数调整最大值,而Float和Double不适用,因涉及小数范围较大。
摘要由CSDN通过智能技术生成

因为根据实践发现大部分的数据操作都集中在值比较小的范围,因此Integer搞了个缓存池,默认范围是-128到127,可以根据通过设置JVM-XX: AutoBoxCacheMax = <size>来修改缓存的最大值,最小值改不了。

实现的原理是int在自动装箱的时候会调用Integer.valueOf,进而用到了IntegerCache。

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

没什么花头,就是判断下值是否在范围之内,如果是的话去 IntegerCache 中取。

IntegerCache在静态块中会初始化好缓存值。

    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 127之内的相等,而超过127的就不等了,因为127之内的就是同一个对象,所以当然相等。

不仅Integer有,Long 也是有的,不过范围是写死的-128到127。

@IntrinsicCandidate
public static Long valueOf(long l) {
  	final int offset = 128;
 	if (l >= -128 && l <= 127) { // will cache
      	return LongCache.cache[(int)l + offset];
   	}
 	return new Long(l);
}

对了Float和 Double是没有滴,毕竟是小数,能存的数太多了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值