title: Difference between == and .equals in Java.
body: <p>I know this has been covered but I've seen inconsistent arguments here on SO.</p>
  
  <p>So if I have:</p>
  
  <pre><code>String a = "apple2e";
  String b = "apple2e";
  
  System.out.println("a==b? " + a == b);
  </code></pre>
  
  <p>I get <strong>FALSE</strong>.</p>
  
  <p>As I understand it, it's because <code>a</code> and <code>b</code> are two different references to the same object (<code>apple2e</code>).</p>
  
  <p>So I would have something like:</p>
  
  <pre><code>a (reference_id 123) ------
                             ---------  "apple2e"
  b (reference_id 456) ------
  </code></pre>
  
  <p>Now, if I just want to compare the <strong>contents</strong> of the two strings, I would use <code>a.equals(b)</code></p>
  
  <p>Does that mean that the JVM is simply returning if the two references are pointing to the same object?  So it's not really doing a character-by-character comparison?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Hold the phones.  Thanks <code>delnan</code> for pointing out the <code>+</code> precedence!!!</p>
  
  <p>When I change it to:</p>
  
  <pre><code>System.out.println(a == b);
  </code></pre>
  
  <p>I indeed get <code>true</code>.</p>
  
  <p>This makes more sense.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I can't believe I didn't catch that.  lol</p>
  
  <p>I was doing:</p>
  
  <pre><code>"a==b? " + a == b
  </code></pre>
  
  <p>Which translates to</p>
  
  <pre><code>"a==b? apple2e" == "apple2e"
  </code></pre>
  
  <p>No wonder it was false!!</p>