Reputation: 605
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";
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[]
Runtime Constant Pool
inside the Method Area
as well ? 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
Reputation: 8541
No expert at this but I will give it a shot.
String text = "Hello World";
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