So, I realize the questions I'm about to ask relate to a topic that has been beaten to death time and time again, however, even after reading all of the answers and documentation I could find, I'm still kind of confused about string interning. Perhaps it's due to my lack of understanding for the JVM; perhaps it's due to the changes introduced in Java 7 depreciating many of the aforementioned answers and documentation. Either way, I'm stuck, and I'm hoping someone can help me understand the concept a bit more clearly...
String a = "text";
String b = new String("text");
In the above example, I understand that two String objects will be created. I also understand that there will be only one char array containing the sequence 't', 'e', 'x', and 't'
in memory.
However, where in memory are each of the string objects actually stored?
If what I've read I've read correctly: the referent of variable a
will be stored in the constant pool whereas the referent of b
will be stored in the heap, right?
If that be the case, I'm confused as to how the intern pool maintains interned strings. Does it keep track of the Strings defined in the constant pool and those that have been manually interned (invoked .intern()
) from the heap? Does the JVM create the string objects defined in the constant pool and load them into the intern pool? I'm confused as to how it all works...
Again, sorry for asking such confusing/asinine questions, it's just that I'm relatively new to the structure and inner-workings of the JVM
and a lot of it has left my head spinning. Thanks!