class D {
public static void main(String args[]) {
Integer b2=128;
Integer b3=128;
System.out.println(b2==b3);
}
}
Output:
false
class D {
public static void main(String args[]) {
Integer b2=127;
Integer b3=127;
System.out.println(b2==b3);
}
}
Output:
true
Note: Numbers between -128 and 127 are true.
Best Answer
When you compile a number literal in Java and assign it to a Integer (capital
I
) the compiler emits:This line of code is also generated when you use autoboxing.
valueOf
is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.From the java 1.6 source code, line 621:
The value of
high
can be configured to another value, with the system property.If you run your program with that system property, it will output true!
The obvious conclusion: never rely on two references being identical, always compare them with
.equals()
method.So
b2.equals(b3)
will print true for all logically equal values of b2,b3.Note that
Integer
cache is not there for performance reasons, but rather to conform to the JLS, section 5.1.7; object identity must be given for values -128 to 127 inclusive.Integer#valueOf(int) also documents this behavior: