title: Comparing Java Boolean class
body: <p>I was stunned to learn that comparing two Boolean Objects with == can get the wrong answer.</p>
  
  <p>Look at the test code below.  Test a and Test c give consistent answers.</p>
  
  <p>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;</p>
  
  <pre><code>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        
      */
  }
  </code></pre>