Integer i1= new Integer(9);
Integer i2= new Integer(9);
if(i1==i2){
System.out.println("true");
}else{
System.out.println("false");
}
int i3=9;
int i4=9;
if(i3==i4){
System.out.println("true");
}else{
System.out.println("false");
}
if(i3==i2){
System.out.println("true");
}else{
System.out.println("false");
}
In Above Code First if-else print false, Why ?.But when second Return true and also third have true.I think wrapper classes(Like double,boolean,char)cant compare True ?
==checks if the two references are referring to the same object, in this case they are not so the==check isfalse. You need to useInteger.equals(), not==:==is correct to use for primitives:intis a primitive.As pointed out by JB Nizet
i2is unboxed to anintwhich makes theifcondition a check between twointprimitives.