Possible Duplicate:
Wrapper class and == operator
It seems as if object equality operator for wrapper classes produces different results depending on whether the wrapped value is in byte range or not. Here is a code snippet to demonstrate this behavior:
System.out.println("smaller than byte");
Integer i1 = 1;
Integer i2 = 1;
if (i1 == i2) System.out.println("same");
if (i1 != i2) System.out.println("not same");
System.out.println("larger than byte");
Integer i3 = 128;
Integer i4 = 128;
if (i3 == i4) System.out.println("same");
if (i3 != i4) System.out.println("not same");
produces the following output:
smaller than byte
same
larger than byte
not same
Note: I got this output on HotSpot (build 1.6.0_24-b07) on linux. Same happens for Long and probably Short (haven't tested it though).
Note: Same output on other HotSpot builds under linux Can anyone explain it?
Small edit, just to make it slightly more interesting:
Adding
if (i3 <= i4 && i3 >= i4) System.out.println("same after all...");
in the end, prints "same after all...
".