0

When i invoke

System.out.println("print this line here");

is the String

"print this line here"

interned?

The class String has a native interning mechanism to look up every new String value (except for those instantiated by the explicit call of its constructor) in a pool of values, and create a String object of that value only if it doesn't find it in this pool.

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 ... ?

//======================

NOTE: this is similar to but different than my prev.Q here.

4
  • Since String objects are immutable, all strings are constants. Commented Nov 12, 2013 at 2:32
  • You just keep repeating what the docs say. What's the point? Commented Nov 12, 2013 at 2:33
  • @PM77-1: Constant, but not necessarily interned; "foo" != new String("foo"). Commented Nov 12, 2013 at 2:34
  • @RussellZahniser - Yes, I know. Commented Nov 12, 2013 at 2:36

3 Answers 3

1

The relevant chapter in the Java Language Specification is here. It states

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.

So, yes, the String literal "print this line here" is interned.

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 ... ?

Not exactly, byte code is a little different. What you will see is the specific reference to a String object in the class' constant pool be pushed on the stack.

You can see this in the byte 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

where the ldc instruction is used to

push a constant #index from a constant pool (String, int or float) onto the stack

That constant is used in the println() method invocation.

Sign up to request clarification or add additional context in comments.

Comments

0

String literals in code are compiled as part of the constant pool of the class. When the class file is first loaded, they are added to the String intern pool. By the time the statement is executed, it is just loading the interned string from the computed constant address.

Edit:

If you do what you suggest:

String str = new String ("print this line here");

... then you still have an interned constant string, plus a new string that is not == to the interned one.

3 Comments

just to beb clear-- it's not interning each & every time during execution(?)
No, it interns once when the class is loaded. The instruction that uses the string is just a load from a fixed address.
correct-- for the edit part btw. made a mistake in ref.to something else on quick-edit up there. removing it.
0

String literals are compile time constants and are default interned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.