Integer中的缓存IntegerCache

本文深入探讨了Java中Integer对象的缓存机制。解释了为什么两个Integer对象在特定值范围内使用==进行比较时会返回true,而在范围外则返回false的现象。通过源码分析揭示了Integer缓存的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知道你有没有遇到过这样的问题?

public class Test {
	public static void main(String[] args) {
		Integer i1 = 100;
	       Integer i2 = 100;
	       System.out.println(i1 == i2);
		Integer i3 = 1000;
	       Integer i4 = 1000;
	       System.out.println(i3 == i4);
	}
}

运行之后的结果是:true,false

考到结果你一定会很惊讶,what?发生了什么?

根据Java编译机制,在编译器编译代码时,在声明的变量上加入了valueOf方法:

public class Test {
	public static void main(String[] args) {
		Integer i1 = Integer.valueOf(100);
		Integer i2 = Integer.valueOf(100);
		System.out.println(i1 == i2);
		Integer i3 = Integer.valueOf(1000);
		Integer i4 = Integer.valueOf(1000);
		System.out.println(i3 == i4);
	}
}

让我们来看看valueOf的实现原理

public static Integer valueOf(String s) throws NumberFormatException {
	return Integer.valueOf(parseInt(s, 10));
}

public static Integer valueOf(int i) {
       if (i >= IntegerCache.low && i <= IntegerCache.high)
       		return IntegerCache.cache[i + (-IntegerCache.low)];//判断是否在缓存数组里,如果在,直接返回
       return new Integer(i);//不在缓存的数组里,直接new一个新的对象返回
}
我们发现,Integer的作者在写这个类时,为了避免重复创建对象,对Integer值做了缓存,如果这个值在缓存范围内,直接返回缓存对象,否则new一个新的对象返回。那究竟这个缓存中有什么内容呢?看一下IntegerCache这个类:
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    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) {//检查虚拟机中有没有相应的配置,如果有取该值和默认值的最大值;如果没有取默认值
            try {
                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);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

        cache = new Integer[(high - low) + 1];//创建缓存数组,并给数组初始化值
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}                                                                                       
}

这是一个内部静态类,该类只能在Integer这个类的内部访问,这个类在初始化的时候,会去加载JVM的配置,如果有值,就用配置的值初始化缓存数组,否则就缓存-128到127之间的值。

再来看之前的代码:

public class Test {
	public static void main(String[] args) {
		Integer i1 = 100;//取同一数组里的对象返回,i1,i2指向同一对象,返回true
	       Integer i2 = 100;
	       System.out.println(i1 == i2);
		Integer i3 = 1000;//这个相当于new了两个对象,==比较的是引用变量的内存地址,所以为false
	       Integer i4 = 1000;
	       System.out.println(i3 == i4);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值