Integer的缓存池

Java Integer缓存池机制解析

缓存池 

在Integer类中存在着缓存池,而缓存池是什么呢?我们先来看一段代码和他对应的结果

        Integer a1=123;
        Integer a2=Integer.valueOf(123);
//
        Integer a3=Integer.valueOf(257);
        Integer a4=257;

        System.out.println("a1==a2 ?" + (a1 == a2));
        System.out.println("a3==a4 ?" + (a3 == a4));

为啥会出现这样呢,这就是因为缓存池的存在,接下来我给大家解析一下这个缓存池的存在。

缓存池的原理

Java 中的 IntegerCache 类实现了一个 缓存池,其目的是为了 避免重复创建相同值的 Integer 对象,从而提高程序的性能和减少内存占用。缓存池中的对象通常是常用的数值范围(默认是 -128127),当 Integer 值在这个范围内时,Java 会直接返回缓存池中的对象,而不是每次都创建一个新的对象。

// Integer.java(部分源码)
private static class IntegerCache {
    static final int low = -128;  // 缓存的最小值,固定为 -128
    static final int high;        // 缓存的最大值,可以通过 JVM 参数调整
    static final Integer cache[]; // 用于存储缓存 Integer 对象的数组

    static {
        int h = 127;  // 默认缓存上界是 127

        // 读取 JVM 启动参数,允许调整缓存上限
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127); // 确保最小是 127
                h = Math.min(i, Integer.MAX_VALUE - (-low) - 1); // 避免h越界
            } catch (NumberFormatException nfe) {
                // 如果解析失败,则保持默认值 127
            }
        }
        high = h; // 赋值最终的 high 值

        // 创建缓存数组,根据low和high的范围创建
        cache = new Integer[(high - low) + 1];
        int j = low;
        for (int k = 0; k < cache.length; k++) {
            cache[k] = new Integer(j++); // 依次填充缓存池
        }

        // 确保 high 至少是 127(Java 规范要求)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {} // 私有构造方法,防止实例化
}

Integer.valueOf()方法对于缓存池的意义

我们先来解读一下对应的源码

    public static Integer valueOf(int i) {
//进行判断传入的值是否在缓存池的范围内,如果存在直接返回对应的值
        if (i >= IntegerCache.low && i <= IntegerCache.high)

            return IntegerCache.cache[i + (-IntegerCache.low)];
//不存在则通过new 关键字创建一个对象出来
        return new Integer(i);
    }

缓存池的意义

1.提高性能

  • 减少对象创建:如果 Integer 变量在 -128 ~ 127 之间,会使用缓存池里的对象,而不是每次 new Integer(),避免不必要的对象创建。
  • 减少垃圾回收:减少 Integer 对象的创建,减少堆内存的压力,降低 GC 频率,提高程序执行效率。

2.提升自动装箱效率 

        Integer对象的创建其实底层就是调用了Integer.valueOf()方法,在缓存池范围中的,直接拿出来用,而不存在的直接将对应的对象创建出来,所以才会发现同一个值,但是不在缓存池范围,进行比较发现是false了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值