Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);
System.out.println("i1=i2 " + (i1 == i2)); //true
System.out.println("i1=i2+i3 " + (i1 == i2 + i3));// true
System.out.println("i1=i4 " + (i1 == i4)); //false
System.out.println("i4=i5 " + (i4 == i5));//false
System.out.println("i4=i5+i6 " + (i4 == i5 + i6));//true
System.out.println("40=i5+i6 " + (40 == i5 + i6)); //true
1 System.out.println(“i1=i2 “ + (i1 == i2)); //true
原因i1和i2 在 -128 到127 之间,同一个cahe
2 System.out.println(“i1=i2+i3 “ + (i1 == i2 + i3));// true
因为i2+i3计算之后直接退化为int
3 System.out.println(“i1=i4 “ + (i1 == i4)); //false
i1 是cahe中的 i4是新new出来的
4 System.out.println(“i4=i5 “ + (i4 == i5));//false
i4 i5 都是new出来的不一样
5 System.out.println(“i4=i5+i6 “ + (i4 == i5 + i6));//true
遇到运算直接拆包,i4==40 当然是true
6 System.out.println(“40=i5+i6 “ + (40 == i5 + i6)); //true
遇到运算直接拆包,40==40 当然是true