Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The relevant chapter in the Java Language Specification is <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5" rel="nofollow">here</a>. It states</p> <blockquote> <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>So, yes, the <code>String</code> literal <code>"print this line here"</code> is interned.</p> <blockquote> <p>I'm wondering how this works with String constants. So, every time i'm invoking this statement in a loop, is "print this line here" being interned-- looked up in the pool to see whether it's there ... ?</p> </blockquote> <p>Not exactly, byte code is a little different. What you will see is the specific reference to a <code>String</code> object in the class' constant pool be pushed on the stack.</p> <p>You can see this in the byte code</p> <pre><code> public static void main(java.lang.String[]) throws java.lang.Exception; Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String print this line here 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return </code></pre> <p>where the <code>ldc</code> <a href="http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings" rel="nofollow">instruction is used to</a></p> <blockquote> <p>push a constant #index from a constant pool (String, int or float) onto the stack</p> </blockquote> <p>That constant is used in the <code>println()</code> method invocation.</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