Integer缓存IntegerCache详解


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

引子

今天,我们从一段非常简单的代码说起。

示例代码

package cn.com;
/**
* 本文作者:谷哥的小弟 
* 博客地址:http://blog.csdn.net/lfdfhl
* 示例描述:Integer缓存IntegerCache
*/
public class IntegerCacheTest {

	public static void main(String[] args) {
		Integer a=9527;
		Integer b=9527;
		System.out.println(a==b);	
		Integer c=97;
		Integer d=97;
		System.out.println(c==d);
	}

}

运行结果

请问,这段代码运行的结果是什么呢?两次输出的结果都是false,对不对?非也!!!

在这里插入图片描述
从上图可以清楚地看到:

  • 1、第一次输出的结果是false
  • 2、第二次输出的结果是true

看此处,就有点丈二和尚摸不着头脑了。这是为什么呢?

源码剖析

在执行Integer a=9527;时会调用Integer类的静态方法public static Integer valueOf(int i)进行自动装箱,其源码如下:

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

该方法的主要逻辑如下:

  • 如果i >= IntegerCache.low && i <= IntegerCache.high则调用IntegerCache.cache[i + (-IntegerCache.low)]
  • 如果i的值不满足i >= IntegerCache.low && i <= IntegerCache.high则调用new Integer(i)

顺着这条主线,我们继续探究Integer缓存IntegerCache。IntegerCache是Integer类中的静态内部类,用于缓存数据便于节省内存、提高性能。其源码如下:

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() {}
    }

从这里我们可以看出 :

  • IntegerCache.low = -128
  • IntegerCache.high = 127
  • 缓冲区cache是一个Integer类型的数组

也就是说:IntegerCache缓存区间为[-128,127]。所以,在调用Integer.valueOf(int i)方法进行自动装箱时假若i的值在[-128,127]区间则生成的Integer对象会被存入缓冲区。当再次对该值进行装箱时会先去缓冲区中获取;如果取到则返回,如果没有取到则创建包装类对象存入缓冲区并返回。

嗯哼,看到这里是不是可以理解之前的那小段代码了呢?

扩展与延伸

除了Integer之外,在其他包装类(例如:Byte,Short,Long等)中也存在类似的设计。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 27
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谷哥的小弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值