In Java, the operator == ALWAYS tests for reference equality.
Originally posted by Burkhard Hassel:
This is correct only when both operands are objects.
But if one of them is a primitive, the other is converted into primitives, you know:
Integer i1 = new Integer (-5000);
int i2 = -5000;
(i1==i2) is true.
1- Two reference variables will result false if they refer to two distinct objects on the heap.
2- Better we use Boolean constants in place of calling constructors to create Boolean object. It saves memory and improves performance.
3- I passed a hard time with the valueOf() method, but finally I got a lot from it.
Originally posted by Richard Boren:
Just when I thought I had a handle on this...
public static final Boolean TRUE1 = new Boolean(true);
public static final Boolean TRUE2 = new Boolean(true);
Two different objects are created with identical content.
Boolean b3 = TRUE1;
Boolean b4 = TRUE2;
Question I have is: Are b3 and b4 autoboxed? If so wouldn�t they both refer to the same object since TRUE1 and TRUE2 have identical primitive values �true�. Or is the �new� retained(?) so that both b3 and b4 are actually referencing different objects which would explain b3==b4 resulting in false.