代码:
public class b {
public static void main(String[] args)
{
Integer a1=new Integer(1000);
Integer a2=new Integer(1000);
Integer a3=new Integer(50);
Integer a4=new Integer(50);
Integer b1=1000;
Integer b2=1000;
Integer b3=50;
Integer b4=50;
int c1=1000;
int c2=50;
System.out.println(a1==a2);//false --new出的对象和缓存不相干
System.out.println(a3==a4);//fasle
System.out.println(a1==b1);//false
System.out.println(a3==b3);//false
System.out.println(b1==b2);//false
System.out.println(b3==b4);//true--(-128-127由于经常用到,会被缓存起来 )缓存,类似于string中的字面量
System.out.println(a1==c1);//true --Integer和int比较会自动拆箱
System.out.println(a3==c2);//true
System.out.println(b1==c1);//true
System.out.println("--------");
String string="hello";
String string2="he"+new String("llo");
System.out.println(string==string2);
}
}
执行结果:
false
false
false
false
false
true
true
true
true
--------
false
主要注意下integer对象在-128和+127范围时的比较。