Java String Immutability & SCP behavior
Source: Dev.to
Why String is Immutable?
Once created, a String object cannot be changed.
String s = new String("Hello");
s.concat("World");
What happens internally?
"Hello"remains unchanged." World"is checked in the String Constant Pool (SCP) and created if not present.- A new object
"Hello World"is created on the heap.

What is String Constant Pool (SCP)?
A String Constant Pool is a special memory area inside the heap where Java stores string literals.
It is used to:
- Avoid duplicate objects
- Save memory
- Improve performance
String s1 = "Java";
String s2 = "Java";
Only one object is created in the SCP; both s1 and s2 reference the same object.
s1 ─┐
├──► "Java" (SCP)
s2 ─┘
What If We Use new?
String s3 = new String("Java");
"Java"is stored in the SCP.- A new
Stringobject is created on the heap. s3points to the heap object.
Thus, two objects exist:
- SCP object (
"Java"literal) - Heap object (created by
new)
Why SCP Exists?
- Without the SCP, every string literal would create a new object, leading to huge memory waste.
- The SCP makes Java memory usage more efficient and improves performance.