Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>==</code> compares the references. The intern method makes sure strings with the same value have the same reference.</p> <p>The javadoc for the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html" rel="noreferrer">String.intern method</a> explains:</p> <blockquote> <p>public String intern()</p> <p>Returns a canonical representation for the string object.</p> <p>A pool of strings, initially empty, is maintained privately by the class String.</p> <p>When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.</p> <p>It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.</p> <p>All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification</p> <p>Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.</p> </blockquote> <p>So without interning the compiler looks at the constants in the java code and builds its constant pool from that. There is a different pool maintained by the String class, and interning checks the string passed in against the pool and makes sure the reference is unique (so that == will work).</p>
 

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