Integer缓存池知道吗?

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

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

java 复制代码
@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在静态块中会初始化好缓存值。

java 复制代码
    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。

java 复制代码
@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是没有滴,毕竟是小数,能存的数太多了。

相关推荐
老胖闲聊18 分钟前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.11822 分钟前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy1 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之1 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?1 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头2 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
年老体衰按不动键盘2 小时前
快速部署和启动Vue3项目
java·javascript·vue
咖啡啡不加糖2 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
liuyang-neu2 小时前
java内存模型JMM
java·开发语言
UFIT2 小时前
NoSQL之redis哨兵
java·前端·算法