String interning is a process of sharing the common memory to the Strings which has same data. This concept is used to save memory and make the execution more efficient. String maintains a pool of Objects. Whenever a new object is created the string class checks in pool for the presence of Object. If the Object is already present then its memory location is returned else the object is added to the pool. The Objects stored always immutable
The string interning is only applicable for String Literals. In creating string Object java is forced to create new object. Whenever a new String literal is created the intern() method is called internally and it checks the pool for the presence of object.
Note :
String Literal
String Object
The only benefit of interning is faster processing
The string interning is only applicable for String Literals. In creating string Object java is forced to create new object. Whenever a new String literal is created the intern() method is called internally and it checks the pool for the presence of object.
Note :
String Literal
String S1 = "qwerty";
String Object
String S4 = new String ("Asdffgg");
The only benefit of interning is faster processing