[Java基础]Integer之间的比较

说实话,在写这篇博客之前,我一直认为Integer 之间的比较 如果字面值相等,那么比较的值一定是相等的(不包括new),直到在某公众号上看到了一篇文档才恍然大悟。

public static void main(String[] args) {
		Integer i = 100;
		Integer i1 = 100;
		Integer i3 = 999;
		Integer i4 = 999;
		int i5 = 100;
		int i6 = 999;
		System.out.println(i == i1);
		System.out.println(i3 == i4);
		System.out.println(i == i5);
		System.out.println(i4 == i6);
	}

执行结果:
在这里插入图片描述

这么几行代码的确很简单,但是执行结果却不是预期的,一开始我认为既然是类,比较的一定是内存地址了,那么
Integer i = 100 这种写法一定会在编译的时候编译成: Integer i = new Integer(100)的吧,既然是new 出来的内存地址指定不一样,那么肯定比较也是false,但是上面的代码中 100 的变量比较却是true,那么上面的解释就行不通了,那么只能利用反编译看看了:
在这里插入图片描述
这么反编译一看,至少明白了两点:

  1. 对于字面常量的Integer,编译的时候会调用valueof(int i)的静态方法及自动装箱
  2. 与整形int比较时会自动拆箱

那么为啥Integer之间的比较会有true和false的两种情况呢?找到valueof的方法看一看:
在这里插入图片描述可以看到有个IntegerCache类,这个方法先进行一个判断,如果判断条件为假,就new 一个新的Integer对象,如果为真就从缓存类的成员变量数组中取出这个值,那么也能说明在数组中的值,等等比较一定是真的,不在那么一定是假的,valueof方法注释的翻译:

/ * *
*返回一个代表指定值的{@code Integer}实例
* {@code int}值。如果没有新的{@code Integer}实例
需要时,一般应优先使用此方法
*构造函数{@link #Integer(int)},因为这个方法是可能的
*以产生更好的空间和时间性能
缓存频繁请求的值。
*
*此方法将始终缓存-128到127的值,
*包括,并可能缓存此范围之外的其他值。
*
* @param是一个{@code int}值。
* @ @返回一个代表{@code i}的{@code Integer}实例。
* @since 1.5
* /

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

这个类上面的注释:

/ * *
*缓存支持对象的自动装箱语义之间的值
* -128和127(包括)根据JLS的要求。
*
*缓存在第一次使用时初始化。缓存的大小
*可由{@code -XX:AutoBoxCacheMax=}选项控制。
*在VM初始化期间,java.lang.Integer.IntegerCache。高属性
的私有系统属性中设置和保存
* sun.misc。VM类。
* /

另外,搜索下IntegerCache可以在parseInt(String s, int radix)找到这么一句话:
在这里插入图片描述
最后提一句,在进行字面值比较的时候还是使用equals方法比较,如果上面的例子使用equals比较时,所有问题将不复存在。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值