Week 149 — What is String interning?
Question of the Week #149
What is String interning?
2 Replies
When multiple parts of the code in an application contain the same
String literal, the JDK uses a single object for all occurances of that literal. This behavior is called interning.
When reading Strings from the console or constructing them from byte[]s, a new String object is created even if one with the same content already exists.
📖 Sample answer from dan1st
string interning is a process to save the string in string pool in java when you make a new string that will create a new object on heap but when you use a string interning that will create a copy in the string pool and the strings will point to this string and if you create a new var have the same string the jvm will search if that string is in the string pool and if it is exists will point to this string and if it is not exists will create a new string in the string pool and will make a point to it
and that will save a memory in heap
Submission from arrm33