JDK 8源码解析——Integer的比较问题

在使用Sting类型时,我们都知道比较2个String类型是否是相同的字符串,需要使用equals()方法,这个可以称为常识。但是对于Integer类型,大家似乎又经常把它当做基本类型,而不是引用类型了,对于比较Integer是否相等,使用的是"=="而不是equals()。这其实是一个误区。例如我们

public class App
{
    public static void main(String[] args)
    {
        Integer integer1 = 9;
        Integer integer2 = 9;
        System.out.println(integer1==integer2);
    }
}

例如上述代码,我们比较的其实不是两个9是否相等,而是比较integer1和integer2所代表的引用地址是否相等。那么这段代码是否输出的就一定是false呢?答案是否定的。

如果将Integer的赋值为129呢?其结果又变为false了。

public class App
{
    public static void main(String[] args)
    {
        Integer integer1 = 129;
        Integer integer2 = 129;
        System.out.println(integer1==integer2);
    }
}

到这里我们先观察一下这段代码的字节码,看看有什么神奇的地方。

Compiled from "App.java"
public class com.lijing.App {
  public com.lijing.App();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: sipush        129
       3: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       6: astore_1
       7: sipush        129
      10: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      13: astore_2
      14: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      17: aload_1
      18: aload_2
      19: if_acmpne     26
      22: iconst_1
      23: goto          27
      26: iconst_0
      27: invokevirtual #4                  // Method java/io/PrintStream.println:(Z)V
      30: return
}

通过对字节码进行分析我们可以知道,在通过等号赋值的时候,实际上是通过调用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);
    }

显而易见的是,Integer维护着一个对象的缓存空间,其取值范围为-128至127,因此才会出现上述情况。因此我们在使用Integer对象的时候,也一定要记住使用equals(),而不是单纯的使用"==",否则有可能出现不相等的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值