Java String Immutability & SCP behavior

Published: (February 19, 2026 at 01:22 PM EST)
2 min read
Source: Dev.to

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.

String immutability illustration

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 String object is created on the heap.
  • s3 points to the heap object.

Thus, two objects exist:

  1. SCP object ("Java" literal)
  2. 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.
0 views
Back to Blog

Related posts

Read more »

Interface in Java

Introduction An interface in Java is used to achieve abstraction and multiple inheritance. It defines what a class should do, but not how it should do it. What...