title: What makes reference comparison (==) work for some strings in Java?
body: <p>I have following lines of codes to compare String. str1 not equal to str2, which is understandable since it compares object reference. But then why s1 is equal to s2? </p>
  
  <pre><code>String s1 = "abc";
  String s2 = "abc";
  
  String str1 = new String("abc");
  String str2 = new String("abc");
  
  if (s1==s2)
      System.out.println("s1==s2");           
  else
      System.out.println("s1!=s2");
  
  if (str1==str2)
      System.out.println("str1==str2");           
  else
      System.out.println("str1!=str2");
  
  if (s1==str1)
      System.out.println("str1==s1");         
  else
      System.out.println("str1!=s1");
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>  s1==s2
    str1!=str2
    str1!=s1 
  </code></pre>