public class test{
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);//true true
//包装类的“==”运算符,在没有遇到算术运算的时候不会
//进行自动拆箱
System.out.println(e == f);//false false
//因为“==”后面有算术运算符,所以自动拆箱
System.out.println(c == (a+b));//false true
System.out.println(c.equals(a+b));//true true
System.out.println(g == (a+b));//false true
//基本数据类型的对象的equals方法不会处理数据类型的转换
//所以g和a+b的数据类型不同,不相等
System.out.println(g.equals(a+b));//true false
}
}
上面这段代码的class文件进行反编译得到如下代码:
public class test
{
public test()
{
}
public static void main(String args[])
{
Integer a = Integer.valueOf(1);
Integer b = Integer.valueOf(2);
Integer c = Integer.valueOf(3);
Integer d = Integer.valueOf(3);
Integer e = Integer.valueOf(321);
Integer f = Integer.valueOf(321);
Long g = Long.valueOf(3L);
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c.intValue() == a.intValue() + b.intValue());
System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
System.out.println(g.longValue() == (long)(a.intValue() + b.intValue()));
System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
}
}