Java小树的参天成长【Integer缓冲区】

# Hello!本文为个人知识总结所得,如有不足还请多多见谅;#

在介绍Integer缓冲区之前,先来看一个面试题,问的是打印的结果是什么 

public class Demo2{
public static void main(string[] args) {
//面试题
	Integer integer1=new Integer(100);
	Integer integer2=new Integer(100);
	System.out.println(integer1==integer2);//?
	
	Integer integer3=100;//白动装箱
	Integer integer4=100;
	System.out.println(integer3==integer4);//?
	
	Integer integer5=200;//自动装箱
	Integer integer6=200;
	System.out.println(integer5==integer6);//?
	}
}

答案:

false
true
false 

你猜对了吗?接下来我会给你讲解为什么会这样。

首先第一个用的是构造方法,我想答案你应该知道是什么原因,这里解释第二和第三。

Integer integer3=100;//自动装箱
Integer integer4=100;

Integer integer5=200;//自动装箱
Integer integer6=200;

其实我们看到的自动装箱详细来说应该是(见代码),也就是说自动装箱其实调用的就是Integer.valueof()。而不是构造方法

 Integer integer3=Integer.valueOf(100);//自动装箱
Integer integer4=Integer.valueOf(100);

Integer integer5=Integer.valueOf(200);//自动装箱
Integer integer6=Integer.valueOf(200);

 而我们答案的原因就出在valueOf()上面,我们打开Integer源码

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

其中的的IntegerCache就是Integer的缓冲区;

其中IntegerCache.low和 IntegerCache.high的数值为-128和127(下面是对应的源码,可以找到对应的值)

  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;

我们也可以从代码中发现一个数组对应代码

 static final Integer[] cache;
 // 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;
        }

可以发现这个数组的长度为high-low+1 = 256;这里面存了256个数据元素,里面的for循环对数组初始化。


回到这串代码,

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

如果i>=-128(i >= IntegerCache.low)     i<127( i <= IntegerCache.high),在这个范围内,那就从Cache数组里面取

回到题目,100在-128与127内,返回的就是缓冲区里面的对象

 return IntegerCache.cache[i + (-IntegerCache.low)];

问题中的integer3和integer4在内存里面如图

 而200没有在这之间,在堆里面开辟空间,和构造方法一样,见代码

 return new Integer(i);

所以答案的原因你懂了吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小智学习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值