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.

gaurs
  • 535
  • 1
  • 4
  • 17
  • String literal is compile-time artifact, mostly. The runtime representation of it is still `String` object. – M. Prokhorov Apr 11 '18 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 ? – gaurs Apr 11 '18 at 16:11
  • https://stackoverflow.com/questions/1855170/when-should-we-use-intern-method-of-string-on-string-literals explains it well – Trinopoty Apr 11 '18 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. – M. Prokhorov Apr 15 '18 at 13:41

1 Answers1

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

zawhtut
  • 7,841
  • 5
  • 46
  • 72
  • But as mentioned [here](https://stackoverflow.com/questions/23252767/string-pool-vs-constant-pool), 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 ? – gaurs Apr 11 '18 at 17:07