String Constant Pool (Storage of Strings)
Tags: java, beginners, tutorial, programming
Published: 2022-02-14 13:51:23

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wj3tokvyz5qvigqhafmz.jpeg)
2) `String s2="Anam";`
In this only **one object** is created i.e. inside string constant pool.


![1 object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmp5wjxh2qojqumuz4pf.jpeg)

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:**

<u>_Can Garbage Collection occur in string constant pool?_</u>

<u>_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._</u>

---

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oarfjzaequvhrs13tppa.jpeg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yklrg31eeny1lta6wvb.jpeg)

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

![Simran](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9xmttdreprjxiocg1b9.jpeg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spm992tr8o91qsm1b5jt.jpeg)

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


![str6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qf49v4gu5ttrjwf26xy.jpeg)

**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.