Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>new String("text");</code> explicitly creates a new and referentially distinct instance of a <code>String</code> object; <code>String s = "text";</code> may reuse an instance from the <strong>string constant pool</strong> if one is available.</p> <p>You <em>very rarely</em> would ever want to use the <code>new String(anotherString)</code> constructor. From the API:</p> <blockquote> <p><a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#String%28java.lang.String%29" rel="noreferrer"><code>String(String original)</code></a> : Initializes a <em>newly created</em> <code>String</code> object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable. </p> </blockquote> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/334518/java-strings-string-s-new-stringsilly"> Java Strings: “String s = new String(”silly“);” </a></li> <li><a href="https://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them"> Strings are objects in Java, so why don’t we use ‘new’ to create them? </a></li> </ul> <hr> <h3>What referential distinction means</h3> <p>Examine the following snippet:</p> <pre><code> String s1 = "foobar"; String s2 = "foobar"; System.out.println(s1 == s2); // true s2 = new String("foobar"); System.out.println(s1 == s2); // false System.out.println(s1.equals(s2)); // true </code></pre> <p><code>==</code> on two reference types is a reference identity comparison. Two objects that are <code>equals</code> are not necessarily <code>==</code>. It is usually wrong to use <code>==</code> on reference types; most of the time <code>equals</code> need to be used instead.</p> <p>Nonetheless, if for whatever reason you need to create two <code>equals</code> but not <code>==</code> string, you <em>can</em> use the <code>new String(anotherString)</code> constructor. It needs to be said again, however, that this is <em>very</em> peculiar, and is rarely the intention.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.21.3" rel="noreferrer">JLS 15.21.3 Reference Equality Operators == and !=</a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29" rel="noreferrer"><code>class Object</code> - <code>boolean Object(equals)</code></a></li> </ul> <h3>Related issues</h3> <ul> <li><a href="https://stackoverflow.com/questions/767372/java-string-equals-versus"> Java String.equals versus == </a></li> <li><a href="https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java">How do I compare strings in Java?</a></li> </ul>
 

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