public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d); //Integer中有一个缓存数组存储[-128,127],超过此范围的数会重新分配内存地址,==比较的是对象的地址
System.out.println(e == f); //321数值太大,不在Integer缓存中
System.out.println(c == (a + b));//包装类的==在遇到算数运算时才会自动拆箱
System.out.println(c.equals(a + b));
System.out.println(g == (a + b));
System.out.println(g.equals(a + b)); //equals不处理数据转型,包装类默认的equals方法会先instanceof进行类型判断,然后通过==比较基本类型的数值
}
运行结果如下:
true
false
true
true
true
false