title: How to compare two Strings when both can be null?
body: <p>I am aware that it is better to call the <code>equals</code> method over using the <code>==</code> operator (see <a href="https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java">this question</a>). I want two strings to compare as equal if they are both null or if they represent the same string. Unfortunately the <code>equals</code> method will throw an <code>NPE</code> if the strings are <code>null</code>. My code is currently:</p>
  
  <pre><code>boolean equals(String s1, String s2) {
    if (s1 == null &amp;&amp; s2 == null) {
      return true;
    }
    if (s1 == null || s2 == null) {
      return false;
    }
    return s1.equals(s2);
  }
  </code></pre>
  
  <p>This is inelegant. What is the correct way to perform this test?</p>