DEV Community

Cover image for String Constant Pool (Storage of Strings)
Tisha Agarwal
Tisha Agarwal

Posted on

String Constant Pool (Storage of Strings)

A string constant pool is a separate place in the heap memory where the values of all the strings which are defined in the program are stored.

Two ways to create string object:

1) String s1=new String("Tisha");
If we create a string like this, then it creates two objects in the memory. One is in Heap Area and the other is in string constant pool.

2 objects
2) String s2="Anam";
In this only one object is created i.e. inside string constant pool.

1 object

That is why it is recommended to make string objects like this: String s2="Anam";, as only one object this created.
More the number of objects, more the project will become heavy.
If the project becomes heavy then it will slow down.


Interview question:

Can Garbage Collection occur in string constant pool?

String objects which are in the string pool will not be garbage collected because a reference variable internally is maintained by JVM for each string literal object. Other String objects will be garbage collected if you don't have reference to it in your program execution.


Special Cases
Suppose if we create two strings str1 and str2
String str1=new String("Alex");--------------->2 objects are created
String str2=new String ("John");-------------->2 objects are created

Special Case

Now if we create one more string with the same string literal as str1 so in that case only one object will be created in heap area.
String constant pool(SCP) will check if that literal is inside it or not. If the literal "Alex" is present inside SCP then it will not create another literal with the same name. Hence only one object is created.
String str3=new String("Alex");----------------->1 object created

SpecialCase2

Now if we create string,
String str4="Simran";-------------->1 object created inside SCP

Simran

To understand it better, let's create more strings and see where their objects are being created- in heap area or in string constant memory?

String str5="Alex";----------->No objects are created
str5 will not create any objects because SCP will first check that whether "Alex" is already present inside it or not.
Since in this case "Alex" is already in SCP so no objects will be created. And str5 will start pointing to the previous "Alex" that existed inside String constant pool(SCP)

str5

str6="Alex";
In this case str6 and str5 both will point to "Alex" inside SCP.

str6

Points to remember:

  • If new keyword is used to create string then object is not only created in String Constant Pool but also in Heap Area.

  • If we create strings like String S="WONDERFUL" , then SCP checks whether the string literal is already present in SCP or not.
    If it is not present the only 1 object is created inside String Constant Pool(SCP)

  • If same literal object is present inside string constant pool (SCP)
    then instead of creating new object with the same literal, it points to the existing object.

Top comments (2)

Collapse
 
omr4125 profile image
ömer

Thanks for sharing

Collapse
 
tishaag098 profile image
Tisha Agarwal

Thank you!