3

As clear from here, the actual characters that comprise a String in Java are actually stored in the private final char value[]; character array.

Can someone please help me in understanding the following questions for an expression String text = "Hello World";

  1. What is actually stored in StringPool ? I've read many places that its the String literal. If thats the case, what exactly is the difference between a String Object and a String literal as both must be backed by a char[]
  2. If the String reference is marked as Constant (final), will it be stored in the Runtime Constant Pool inside the Method Area as well ?
  3. As mentioned here, the actual char[] value is created on the heap only. So when intern() method is called on a String object created using new operator, what will be copied to the StringPool (assuming its not the character array) ?

Edit :

As mentioned here, StringPool is implemented as a HashMap. So what could be the key and value for the same ? It appears that the value cannot be the char[] value as the same is created on the Heap.

4
  • String literal is compile-time artifact, mostly. The runtime representation of it is still String object. Commented Apr 11, 2018 at 16:01
  • @M.Prokhorov : If String literal is just a compile time artifact, then how is it moved to StringPool when intern method is called on a String created using new operator as it should not be there at all ? Commented Apr 11, 2018 at 16:11
  • stackoverflow.com/questions/1855170/… explains it well Commented Apr 11, 2018 at 16:20
  • @Sumit, I was anwering on the difference between string object and string literal: there is none, at runtime. At runtime, both are just String objects. Certain number of those are created from literals, but that's about it. Javadoc for intern() is also pretty clear as to what is actually stored in the pool and is returned returned from that method. It's not concerned with char[] inside string, because a String is not actually required to have that array inside it, that's just its implementation detail. Commented Apr 15, 2018 at 13:41

1 Answer 1

1

No expert at this but I will give it a shot.

String text = "Hello World";

enter image description here

enter image description here

lcd(load a constant) look ups the string in a constant pool table. found #2 which has an address of "Hello Word" which is at #14

Reference:What is the purpose of the Java Constant Pool?

Reference:https://www.ibm.com/developerworks/library/it-haggar_bytecode/#opcode

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

1 Comment

But as mentioned here, a StringPool is different than a ConstantPool. The load constant command (lcd) refers to the constants in Constant Pool symbol table but how the String literal, String Object and Underlying Character array are handled memory wise ?

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.