Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It compares references - i.e. are both variables referring to the exact same object (rather than just equal ones).</p> <ul> <li><code>s</code> and <code>s2</code> refer to different objects, so the expression evaluates to false.</li> <li><code>s</code> and <code>s1</code> refer to the <em>same</em> objects (as each other) because of the assignment.</li> <li><code>s2</code> and <code>s3</code> refer to the same objects (as each other) because of string interning.</li> </ul> <p>If that doesn't help much, please ask for more details on a particular bit. Objects and references can be confusing to start with.</p> <p>Note that only string <em>literals</em> are interned by default... so even though <code>s</code> and <code>s2</code> refer to <em>equal</em> strings, they're still two separate objects. Similarly if you write:</p> <pre><code>String x = new String("foo"); String y = new String("foo"); </code></pre> <p>then <code>x == y</code> will evaluate to false. You can <em>force</em> interning, which in this case would actually return the interned literal:</p> <pre><code>String x = new String("foo"); String y = new String("foo"); String z = "foo"; // Expressions and their values: x == y: false x == z: false x.intern() == y.intern(): true x.intern() == z: true </code></pre> <p>EDIT: A comment suggested that <code>new String(String)</code> is basically pointless. This isn't the case, in fact.</p> <p>A <code>String</code> refers to a <code>char[]</code>, with an offset and a length. If you take a substring, it will create a new String referring to the <em>same</em> <code>char[]</code>, just with a different offset and length. If you need to keep a small substring of a long string for a long time, but the long string itself isn't needed, then it's useful to use the <code>new String(String)</code> constructor to create a <em>copy</em> of just the piece you need, allowing the larger <code>char[]</code> to be garbage collected.</p> <p>An example of this is reading a dictionary file - lots of short words, one per line. If you use <code>BufferedReader.readLine()</code>, the allocated char array will be at least 80 chars (in the standard JDK, anyway). That means that even a short word like "and" takes a char array of 160 bytes + overheads... you can run out of space pretty quickly that way. Using <code>new String(reader.readLine())</code> can save the day.</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