Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>==</code> tests for reference equality (whether they are the same object).</p> <p><code>.equals()</code> tests for value equality (whether they are logically "equal"). </p> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#equals(java.lang.Object,%20java.lang.Object)" rel="noreferrer">Objects.equals()</a> checks for <code>null</code> before calling <code>.equals()</code> so you don't have to (available as of JDK7, also available in <a href="https://github.com/google/guava/wiki/CommonObjectUtilitiesExplained#equals" rel="noreferrer">Guava</a>).</p> <p><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contentEquals-java.lang.CharSequence-" rel="noreferrer">String.contentEquals()</a> compares the content of the <code>String</code> with the content of any <code>CharSequence</code> (available since Java 1.5).</p> <p>Consequently, if you want to test whether two strings have the same value you will probably want to use <code>Objects.equals()</code>.</p> <pre><code>// These two have the same value new String("test").equals("test") // --&gt; true // ... but they are not the same object new String("test") == "test" // --&gt; false // ... neither are these new String("test") == new String("test") // --&gt; false // ... but these are because literals are interned by // the compiler and thus refer to the same object "test" == "test" // --&gt; true // ... string literals are concatenated by the compiler // and the results are interned. "test" == "te" + "st" // --&gt; true // ... but you should really just call Objects.equals() Objects.equals("test", new String("test")) // --&gt; true Objects.equals(null, "test") // --&gt; false Objects.equals(null, null) // --&gt; true </code></pre> <p>You almost <strong>always</strong> want to use<code>Objects.equals()</code>. In the <strong>rare</strong> situation where you <strong>know</strong> you're dealing with <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#intern--" rel="noreferrer">interned</a> strings, you <em>can</em> use <code>==</code>.</p> <p>From <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5" rel="noreferrer">JLS 3.10.5. <em>String Literals</em></a>:</p> <blockquote> <p>Moreover, a string literal always refers to the <em>same</em> instance of class <code>String</code>. This is because string literals - or, more generally, strings that are the values of constant expressions (<a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28" rel="noreferrer">§15.28</a>) - are "interned" so as to share unique instances, using the method <code>String.intern</code>.</p> </blockquote> <p>Similar examples can also be found in <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#d5e1634" rel="noreferrer">JLS 3.10.5-1</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload