深入理解Integer源码

今天,我们来理解一下,我们经常在开发中使用的Integer引用数据类型:

先抛出问题:

Integer i = 127;
Integer c = 127;
System.out.println(i == c);

我们运行的结果是:

true

我们又试着使用下面的代码运行:

Integer i = 128;
Integer c = 128;
System.out.println(i == c);

然而现在的结果出乎我们意料:

false

这是为什么呢,我们使用debug查看原因(我事先翻译了一下)

/**
  * 返回一个表示指定的{@code Integer}实例
  * {@code int}值。如果一个新的{@code Integer}实例不是
  * 必要时,一般应优先使用此方法
  * 构造函数{@link #Integer(int)},
  * 就像这个方法一样产生明显更好的空间和时间性能缓存频繁请求的值。
  * <p>
  * 该方法缓存的值总是在-128到127之间,
  * 包含,并可能缓存该范围之外的其他值。
  * JDK 1.5
  */
public static Integer valueOf(int i) {
    //该方法缓存的值总是在-128到127之间
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

可以看出他是使用IntegerCache中的静态变量那么我们 来查看一下这个类里面到底是什么为什么是在-128到127之间

  /**
    * 缓存支持对象标识语义的自动装箱之间的值
    * 根据JLS要求-128和127(含)
    * <p>
    * 缓存在第一次使用时被初始化。缓存的大小由{@code -XX:AutoBoxCacheMax=<size>}选项控制。
    * 在虚拟机初始化时,java.lang.Integer.IntegerCache。高属性
    * 可以设置和保存在私有系统属性在
    * sun.misc。VM类。
    */
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];
        
        static {
            // 高值可由属性配置
            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);
                    // 最大数组大小为 Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) - 1);
                } catch (NumberFormatException nfe) {
                    // 如果无法将属性解析为int,请忽略它。
                }
            }
            high = h;//high=127;
            //计算结果[127-(-128)+1]=256;
            cache = new Integer[(high - low) + 1];
            int j = low;//int j=-128;
            for (int k = 0; k < cache.length; k++)
                //cache[256]=重新创建了一个对象
                cache[k] = new Integer(j++);
            
            // 范围[-128,127]必须是内部的(JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
        // 不允许创建对象
        private IntegerCache() {
        }
    }

​ 我们看到注释中写到:缓存在第一次使用时被初始化。缓存的大小由{@code -XX:AutoBoxCacheMax=}选项控制。,既然缓存由**-XX:AutoBoxCacheMax=**来控制,那我们是不是来设置他的缓存大小:那我们就尝试来设置一下他的缓存大小

image-20210117104825447

image-20210117104942156

然后我们在来运行一下我们的代码,结果是:

true

具体的分析内容可以查看上的代码注释

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LaoSum

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

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

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

打赏作者

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

抵扣说明:

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

余额充值