Consider this code:
class test {
public static void main(String[] args) {
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );
}
public void method( Integer i , Integer eye ) {
System.out.println(i == eye );
}
}
It prints:
false
true
false
I understand the first false
, the == operator only checks if two references are working on the same object, which in this case aren't.
The following true
and false
have me scratching my head. Why would Java consider i3
and i4
equal but i1
and i2
different? Both have been wrapped to Integer, shouldn't both evaluate to false? Is there a practical reason for this inconsistency?