title: Is it safe to compare two `Integer` values with `==` in Java?
body: <p>I have this Java code:</p>
  
  <pre><code>public class Foo {
      public static void main(String[] args) {
           Integer x = 5;
           Integer y = 5;
           System.out.println(x == y);
      }
  }
  </code></pre>
  
  <p>Is it guaranteed to print <code>true</code> on the console? I mean, is it comparing the two boxed integers by value (which is what I need to do) or by reference identity?</p>
  
  <p>Also, will it be any different if I cast them to unboxed integers like this</p>
  
  <pre><code>public class Foo {
      public static void main(String[] args) {
           Integer x = 5;
           Integer y = 5;
           System.out.println((int) x == (int) y);
      }
  }
  </code></pre>