Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>==</code> operator will check for reference equality, that is, will return <code>true</code> if the two argument <code>String</code>s are the same instance.</p> <p>Whenever a <code>String</code> literal (for instance <code>"Hello"</code>) occurs in a class, a <code>String</code> instance is <em>interned</em> (kind of stored in an internal cache so it can be reused). </p> <p>After doing <code>String txt1="Hello"</code>, <code>txt1</code> will be very same reference of the interned <code>String</code>. So, </p> <pre><code>String txt1="Hello"; String txt2="Hello"; </code></pre> <p>Results in <code>txt1</code> and <code>txt2</code> being the same instance, that is, the interned one. </p> <p>When you're doing <code>String txt1=new String("Hello")</code>, it's calling the <code>String</code> constructor with the interned instance as an argument (kind of a copy constructor). So, <code>txt1</code> will be a <strong>new</strong> <code>String</code> instance holding the same value as the interned instance, and the <code>==</code> operator will return <code>false</code>.</p> <p>More information on the subject can be found in the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5" rel="nofollow noreferrer">3.10.5. String Literals</a> section of the JLS. </p> <blockquote> <p>A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).</p> <p>Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern. </p> </blockquote> <p>The following question's answer explain <a href="https://stackoverflow.com/q/3451145/851811">When are Java Strings interned?</a>. The following link elaborates on the subject: <a href="http://javatechniques.com/blog/string-equality-and-interning/" rel="nofollow noreferrer">String Equality and Interning</a>.</p> <p>As a side note, remember to use <code>equals()</code> in order to perform String comparisons based on their contents.</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. 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