Integer lInt1 = 5000;
Integer lInt2 = 5000;
if (lInt1 != lInt2) {
System.out.println("Different Objects");
}
Integer lInt3 = 100;
Integer lInt4 = 100;
if (lInt3 == lInt4) {
System.out.println("Same Object");
}
When compiled and run, the output will be:
Different Objects
Same Object
Apparently, in order to save memory, when using autoboxing to create two instances of the Integer (and Short) wrapper objects, they will be always == if the primitive values that they hold are from -128 to 127. Why on earth would the Java gods only allow this to happen for values from that interval, and not make it for any (legal) value possible?
No comments:
Post a Comment