Integer的缓存机制

一 现象

在引入Integer的缓存机制前,可以先判断一下以下几种情况

	    # 一:自动装箱
        Integer s1 = 2;
        Integer s2 = 2;
        System.out.println(s1 == s2);
        # 答案为true
       	
		# 二:
		Integer s1 = new Integer(2);
		Integer s2 = new Integer(2);
		System.out.println(s1 == s2);
		# 答案为false

情况二很好理解,虽然传值相同,但是Integer是包装类,不同对象的引用地址是不一样的,而“==” 比的是引用地址,那为什么情况一中会得到结果true
这里就不得不提到Integer的缓存机制了

二 Integer的缓存机制

2.1 自动装箱等效于valueOf

#以上代码的情况一的自动装箱等效于Integer中的ValueOf()方法,如下:
	Integer s1 = Integer.valueOf(2);
	Integer s2 = Integer.valueOf(2);
	System.out.println(s1 == s2);

2.2 valueOf

以下是valueOf()方法的具体实现

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

可以看见,其实现方式和IntegerCache有关

2.3 IntegerCache

IntegerCache是Interger的一个静态内部类,实现如下:

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;
        }

可以看到,默认情况下,该静态内部类会直接缓存-127到128的Integer对象,因此,在valueOf()方法中,如果值在-127-128之间,都会直接返回缓存中的该对象而不会重新生成对象,引用地址当然相同了。
而且java5引入时,该范围还是固定的,而在java6之后,Integer的缓存中还可以通过Integer.IntegerCache.high来设置最大值。由于这个缓存机制,程序中第一次使用Integer的时候还需要一定的时间家长该缓存类

三 为什么要有缓存机制

3.1 原因

因为我们常见的基本数据类型中,使用包装类包装数值时会创建大量对象,如果没有缓存的话,会有大量的包装类被创建,占用内存,降低效率。选择最常用的数值范围设置缓存机制,就可以优化这一现象

3.2 其他包装对象的缓存

既然缓存机制的原因我们知道了,那除了Integer之外,其他包装类有这种缓存机制吗?肯定是有的

  • ByteCache:缓存Byte对象
  • ShortChche:缓存Short对象
  • LongChche:缓存Long对象
  • CharacterChche:缓存Character对象
    Byte,Short,Long的缓存范围都是-128-127,Character的缓存范围是0-127,除了Integer,其他的缓存范围都是固定的
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

油光发亮的小猛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值