I was stunned to learn that comparing two Boolean Objects with == can get the wrong answer.
Look at the test code below. Test a and Test c give consistent answers.
Test b fails. It seems that new Boolean(true) can create a separate object with the same value, instead of returning a reference to Boolean.TRUE;
public static void main(String[] args) {
Boolean a = Boolean.TRUE;
Boolean b = new Boolean(true);
Boolean c = null;
boolean x = true;
boolean y = false;
System.out.println("Test a");
System.out.println(( a == Boolean.TRUE ) ? "TRUE" : "FALSE");
System.out.println(( Boolean.TRUE.equals(a)) ? "TRUE" : "FALSE");
System.out.println("Test b");
System.out.println(( b == Boolean.TRUE ) ? "TRUE" : "FALSE");
System.out.println(( Boolean.TRUE.equals(b)) ? "TRUE" : "FALSE");
System.out.println("Test c");
System.out.println(( c == Boolean.TRUE ) ? "TRUE" : "FALSE");
System.out.println(( Boolean.TRUE.equals(c)) ? "TRUE" : "FALSE");
/* OUTPUT is
Test a
TRUE
TRUE
Test b
FALSE
TRUE
Test c
FALSE
FALSE
*/
}
Because
Booleanis a reference type and==tests if they are the same object in memory then you get false because you allocatedbwithnew.