Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does string interning work in Java 7+? [duplicate]

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!

like image 758
kylemart Avatar asked Nov 30 '14 05:11

kylemart


2 Answers

There's a thing called String Memory Pool in java, when you declare:

String str1="abc";

It goes to that memory pool and not on the heap. But when you write:

String str2=new String("abc");

It creates a full fledged object on the heap, If you again write:

String str3 = "abc"; 

It won't create any more object on the pool, it will check the pool if this literal already exists it will assign that to it. But writing:

String str4 = new String("abc");

will again create a new object on the heap

Key point is that:

A new object will always be created on the heap as many times as you keep writing:

new String("abc");

But if you keep assigning the Strings directly without using the keyword new, it will just get referenced from the memory pool (or get created if not present in the memory pool)

intern() method finds if the string is present in the memory pool if it is not it adds it to the memory pool and returns a reference to it. so after using this method the String reference of yours is not pointing to any object on the heap, it is pointing to an object in the String Memory Pool (Also, note that the memory pool only contains unique strings).

like image 139
Sarthak Mittal Avatar answered Sep 23 '22 23:09

Sarthak Mittal


When you say new String() you get a new Object reference so consider

String a = "text";
String b = new String("text");
System.out.println(a == b);
b = b.intern();
System.out.println(a == b);

Then first a == b will display false because they are different references. If we intern() b by saying b = b.intern() we can then test again and get true. I hope that helps. The above code has worked the same way in Java since version 1.0 (and it still works this way in Java 8 today).

like image 41
Elliott Frisch Avatar answered Sep 24 '22 23:09

Elliott Frisch