面试官:为什么Integer用==比较时127相等而128不相等?

这个几乎是Java 5引入自动装箱和自动拆箱后,很多人都会遇到(而且不止一次),而又完全摸不着头脑的坑。虽然已有很多文章分析了原因,但鉴于我这次还差点坑了同学,还是纪录下来长点记性。

问题描述

例一

来个简单点的例子

public static void main(String[] args) {
    for (int i = 0; i < 150; i++) {
        Integer a = i;
        Integer b = i;
        System.out.println(i + " " + (a == b));
    }
}

i取值从0到150,每次循环a与b的数值均相等,输出a == b。运行结果:

0 true
1 true
2 true
3 true
...
126 true
127 true
128 false
129 false
130 false
...

从128开始a和b就不再相等了。

这个例子还容易看出来涉及到int的自动装箱和自动拆箱,下面来个不太容易看出来的。

例二

public static void main(String[] args) {
    Map<Integer, Integer> mapA = new HashMap<>();
    Map<Integer, Integer> mapB = new HashMap<>();
    for (int i = 0; i < 150; i++) {
        mapA.put(i, i);
        mapB.put(i, i);
    }
    for (int i = 0; i < 150; i++) {
        System.out.println(i + " " + (mapA.get(i) == mapB.get(i)));
    }
}

i取值从0到150,mapA和mapB均存储(i, i)数值对,输出mapA的值与mapB的值的比较结果。运行结果:

0 true
1 true
2 true
3 true
...
126 true
127 true
128 false
129 false
130 false
...
为什么两个例子都是从0到127均显示两个变量相等,而从128开始不相等?

原因分析

自动装箱

首先回顾一下自动装箱。对于下面这行代码

Integer a = 1;

变量a为Integer类型,而1为int类型,且Integer和int之间并无继承关系,按照Java的一般处理方法,这行代码应该报错。

但因为自动装箱机制的存在,在为Integer类型的变量赋int类型值时,Java会自动将int类型转换为Integer类型,即

Integer a = Integer.valueOf(1);

valueOf()方法返回一个Integer类型值,并将其赋值给变量a。这就是int的自动装箱。

是同一个对象吗?

再看最开始的例子:

public static void main(String[] args) {
    for (int i = 0; i < 150; i++) {
        Integer a = i;
        Integer b = i;
        System.out.println(i + " " + (a == b));
    }
}

每次循环时,Integer a = i和Integer b = i都会触发自动装箱,而自动装箱会将int转换Integer类型值并返回;我们知道Java中两个new出来的对象因为时不同的实例,无论如何==都会返回fasle。比如

new Integer(1) == new Integer(1);

就会返回false。

那么例子中Integer a = i和Integer b = i自动装箱产生的变量a和b就不应该时同一个对象了,那么==的结果应该时false。

128以上为false容易理解,但为何0到127时返回true了呢?==返回true的唯一情况是比较的两个对象为同一个对象,那不妨把例子中a和b的内存地址都打印出来看看:

for(int i=0;i<150;i++){
    Integer a=i;
    Integer b=i;
    System.out.println(a+" "+b+" "+System.identityHashCode(a)+" "+System.identityHashCode(b));
}

identityHashCode()方法可以理解为输出对应变量的内存地址,输出为:

0 0 762119098 762119098
1 1 1278349992 1278349992
2 2 1801910956 1801910956
3 3 1468253089 1468253089
...
126 126 1605164995 1605164995
127 127 1318497351 1318497351
128 128 101224864 479240824
129 129 1373088356 636728630
130 130 587071409 1369296745
...

竟然从0到127不同时候自动装箱得到的是同一个对象!从128开始才是正常情况。

看看源码

“从0到127不同时候自动装箱得到的是同一个对象”就只能有一种解释:自动装箱并不一定new出新的对象。

既然自动装箱涉及到的方法是Integer.valueOf(),不妨看看其源代码:

/**
    * Returns an {@code Integer} instance representing the specified
    * {@code int} value.  If a new {@code Integer} instance is not
    * required, this method should generally be used in preference to
    * the constructor {@link #Integer(int)}, as this method is likely
    * to yield significantly better space and time performance by
    * caching frequently requested values.
    *
    * This method will always cache values in the range -128 to 127,
    * inclusive, and may cache other values outside of this range.
    *
    * @param  i an {@code int} value.
    * @return an {@code Integer} instance representing {@code i}.
    * @since  1.5
    */
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

其注释里就直接说明了-128到127之间的值都是直接从缓存中取出的。看看是怎么实现的:如果int型参数i在IntegerCache.low和IntegerCache.high范围内,则直接由IntegerCache返回;否则new一个新的对象返回。似乎IntegerCache.low就是-128,IntegerCache.high就是127了。

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

果然在其static块中就一次性生成了-128到127直接的Integer类型变量存储在cache[]中,对于-128到127之间的int类型,返回的都是同一个Integer类型对象。

这下真相大白了,整个工作过程就是:Integer.class在装载(Java虚拟机启动)时,其内部类型IntegerCache的static块即开始执行,实例化并暂存数值在-128到127之间的Integer类型对象。当自动装箱int型值在-128到127之间时,即直接返回IntegerCache中暂存的Integer类型对象。

为什么Java这么设计?我想是出于效率考虑,因为自动装箱经常遇到,尤其是小数值的自动装箱;而如果每次自动装箱都触发new,在堆中分配内存,就显得太慢了;所以不如预先将那些常用的值提前生成好,自动装箱时直接拿出来返回。哪些值是常用的?就是-128到127了。

解决方法

既然我们的目的是比较数值是否相等,而非判断是否为同一对象;而自动装箱又不能保证同一数值的Integer一定是同一对象或一定不是同一对象,那么就不要用==,直接用equals()好了。实际上,Integer重写了equals()方法,直接比较对象的数值是否相等。

for (int i = 0; i < 150; i++) {
    Integer a = i;
    Integer b = i;
    System.out.println(i + " " + (a.equals(b)));
}

这样返回值就全都是true了。

备注

不仅int,Java中的另外7中基本类型都可以自动装箱和自动拆箱,其中也有用到缓存。见下表:

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Java中,'=='和'equals()'方法都可以用于比较两个对象是否相等,但是它们之间有一些区别。 当比较两个基本数据类型,例如'int',使用'=='是可以的,因为它们的值可以直接比较。然而,当比较两个对象,'=='比较的是对象的引用是否相等,即它们是否指向相同的内存地址。而'equals()'方法比较的是对象的内容是否相等,即它们在逻辑上是否相等。 对于'Integer'类,'=='只有在比较两个相同的'Integer'对象才会返回true。这是因为对于小的整数值,Java会将其缓存起来并重用同一个对象,但对于大的整数值则不会缓存。因此,如果比较两个大的整数值,'=='可能会返回false。而使用'equals()'方法则可以比较两个'Integer'对象的值是否相等。 综上所述,如果要比较两个'Integer'对象的值是否相等,应该使用'equals()'方法。 ### 回答2: 在Java语言中,integer比较应该使用equals方法,而不是==操作符。因为Integer是一个类,它将int数据类型装箱为一个包装类对象,此使用==操作符比较的是Integer对象的引用地址,而不是它们存储的值。因此,如果您使用==操作符比较两个Integer对象,您将得到与您想要的结果不同的结果。 例如,下面这个例子比较了两个Integer对象: Integer a = new Integer(10); Integer b = new Integer(10); if (a == b) { System.out.println("a == b"); } else { System.out.println("a != b"); } 运行结果将是a != b,即使a和b都存储值10,因为它们是两个不同的对象。 相反,equals方法比较的是两个对象存储的值,而不是它们的引用地址。因此,正确的比较两个Integer对象的方法是使用equals方法,如下所示: Integer a = new Integer(10); Integer b = new Integer(10); if (a.equals(b)) { System.out.println("a equals b"); } else { System.out.println("a not equals b"); } 现在将输出a equals b,因为equals方法比较的是这两个对象存储的值,它们都是10。 ### 回答3: 在Java语言中,integer类型是一个对象,可以用“==”和equals方法进行比较。 使用“==”进行比较比较的是引用地址,即判断两个Integer对象是否指向同一个内存地址。如果两个Integer对象指向同一个地址,返回true,否则返回false。 而使用equals方法进行比较,则是比较两个Integer对象的值是否相等。如果相等,返回true,否则返回false。这是因为Integer类重写了Object类的equals方法,使其比较对象的值。 因此,如果需要比较Integer对象的值是否相等,应该使用equals方法。如果需要比较两个引用是否指向同一个对象,应该使用“==”进行比较。 需要注意的是,当使用“==”比较两个小于等于127的整数,不一定会返回false,这是因为Java中对于小于等于127的整数,会有一个缓存池,将这些整数的对象缓存下来以便重复使用,所以使用“==”比较这些整数,可能会返回true。但这并不代表使用“==”比较Integer对象就是正确的做法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值