First code:
Integer i1 = 1234;
Integer i2 = 1234;
System.out.println(i1 == i2); //false
System.out.println(i1.equals(i2)); //true
Although it seems that primitive types are being used, they are actually objects, so when these objects are compared using ==
the result is false
, since they are different instances of Integer
. So far so good.
Second code:
Integer i1 = 123;
Integer i2 = 123;
System.out.println(i1 == i2); //true
System.out.println(i1.equals(i2)); //true
Why is the result of the first comparison being true
and not false
as in the first code?