首页 > 学院 > 开发设计 > 正文

再谈包装类Integer对象的比较

2019-11-15 01:13:34
字体:
来源:转载
供稿:网友
再谈包装类Integer对象的比较
public class CompareDemo {    public static void main(String[] args) {        int a = 128, b = 128;        System.out.PRintln(a == b); // true                 Integer c = 128, d = 128;        System.out.println(c == d); // false                System.out.println(c.equals(d)); // true                 Integer e = -128, f = -128;        System.out.println(e == f); // true    }}

我们在命令行执行一下

1. javac CompareDemo.java

2. javap -c CompareDemo

部分截图如下:

可以看到在 在执行Integer c = 128, d = 128; 时调用了两次 Integer.valueOf()函数。

所以语句Integer c = 128 ; 等同于Integer c =Integer.valueOf(128);

Integer d = 128 ;等同于Integer d =Integer.valueOf(128);

我们从源码中看下这个函数的实现:

public static Integer valueOf(int i) {    final int offset = 128;    if (i >= -128 && i <= 127) { // must cache         return IntegerCache.cache[i + offset];    }        return new Integer(i);    }

很明显当i >= -128 且 i <= 127 时,我们返回的是缓存中的Integer的对象。

内部类 IntegerCache 定义如下:

private static class IntegerCache {    private IntegerCache(){}    static final Integer cache[] = new Integer[-(-128) + 127 + 1];    static {        for(int i = 0; i < cache.length; i++)        cache[i] = new Integer(i - 128);    }    }

这就解释了为什么 c == d 返回的为什么是false了, c,d 这里都是对象,== 比较的是引用的地址,

c.equals(d) 才是对象中值的比较。正是因为c,d 的128超出了IntegerCache缓存的范围,所以c,d

是两个不同的对象,自然引用地址也不同。

而e , f 的-128 则在缓存的范围内,故返回的是同一个对象。引用地址相同。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表