Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no difference in this case.</p> <p>It's important to realize how references works, and that local variables are distinct from the objects they are referring to. The memory required by local variables themselves are insignificant; you should never hesitate from declaring local variables if it makes the code more readable.</p> <p>Consider for example the following code:</p> <pre><code>String s1 = "a very long string..."; String s2 = s1; </code></pre> <p>This code declares two <code>String</code> references, but they both refer to the same <code>String</code> object. The memory requirement is not doubled in cases like this.</p> <p>You should never underestimate how smart the compiler can be at optimizing codes. Consider the following for example:</p> <pre><code>System.out.println("Hello world!"); System.out.println("Hello world!"); System.out.println("Hello world!"); </code></pre> <p>The above snippet in fact does <em>NOT</em> store the string object <code>"Hello world!"</code> three times in memory! The literal is <em>interned</em> and only stored once in memory.</p> <h3>References</h3> <p><a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.10.5" rel="nofollow noreferrer">JLS 3.10.5 String Literals</a></p> <blockquote> <p>Each string literal is a reference to an instance of <code>class String</code>. <code>String</code> objects have a constant value. String literals --or, more generally, strings that are the values of constant expressions-- are "interned" so as to share unique instances, using the method <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern%28%29" rel="nofollow noreferrer"><code>String.intern</code></a>.</p> </blockquote> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/3359829/what-is-the-advantage-of-string-object-as-compared-to-string-literal">what is the advantage of string object as compared to string literal</a> <ul> <li>On <code>"string"</code> vs <code>new String("string")</code></li> </ul></li> <li><a href="https://stackoverflow.com/questions/1091045/is-it-good-practice-to-use-java-lang-string-intern">Is it good practice to use java.lang.String.intern()?</a></li> <li><a href="https://stackoverflow.com/questions/1530864/java-why-doesnt-my-string-comparison-work">Java: Why doesn’t my string comparison work?</a></li> <li><a href="https://stackoverflow.com/questions/767372/java-string-equals-versus"> Java String.equals versus == </a></li> </ul>
    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.
    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