gaurs
gaurs

Reputation: 605

String Memory Model - java6

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.

Upvotes: 3

Views: 99

Answers (1)

zawhtut
zawhtut

Reputation: 8541

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

Upvotes: 1

Related Questions