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

相关推荐
你喜欢喝可乐吗?11 分钟前
RuoYi-Cloud 验证码处理流程
java·spring cloud·微服务·vue
三体世界38 分钟前
TCP传输控制层协议深入理解
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
Java技术小馆40 分钟前
langChain开发你的第一个 Agent
java·面试·架构
kangkang-41 分钟前
PC端基于SpringBoot架构控制无人机(二):MavLink协议
java·spring boot·后端·无人机
Dcs1 小时前
Anthropic 爆严重安全漏洞!程序员机器沦陷
java
随心点儿1 小时前
使用python 将多个docx文件合并为一个word
开发语言·python·多个word合并为一个
不学无术の码农1 小时前
《Effective Python》第十三章 测试与调试——使用 Mock 测试具有复杂依赖的代码
开发语言·python
tomcsdn311 小时前
SMTPman,smtp的端口号是多少全面解析配置
服务器·开发语言·php·smtp·邮件营销·域名邮箱·邮件服务器
EnigmaCoder1 小时前
Java多线程:核心技术与实战指南
java·开发语言
攀小黑1 小时前
阿里云 使用TST Token发送模板短信
java·阿里云