int num = 1234;
Integer num_integer = new Integer(1234);
Integer num_valueOf = Integer.valueOf(1234);
System.out.println(num == num_integer);
System.out.println(num == num_valueOf);
System.out.println(num_integer == num_valueOf);
System.out.println(num_integer.equals(num_valueOf));
true: 拆箱后,比较int,数值相同
true: 拆箱后,比较int,数值相同
false: 对象比较,地址不同
true: Integer.equals比较相同
Integer num_a = new Integer(1234);
Integer num_b = new Integer(1234);
System.out.println(num_a == num_b);
System.out.println(num_a.equals(num_b));
false: 对象比较,地址不同
true: Integer.equals比较相同
Integer num_a = Integer.valueOf(1234);
Integer num_b = Integer.valueOf(1234);
System.out.println(num_a == num_b);
System.out.println(num_a.equals(num_b));
false: 对象比较,地址不同
true: Integer.equals比较相同
Integer num_a = Integer.valueOf(123);
Integer num_b = Integer.valueOf(123);
System.out.println(num_a == num_b);
System.out.println(num_a.equals(num_b));
true: -128~ 127之间的Integer.valueOf会使用缓存,所以是同一对象
true: Integer.equals比较相同
结论: Integer比较数值是否相等,建议使用Integer.intValue() 或者 equals()方法