Member-only story
What is String Constant Pool in Java? | Explained with Examples
Learn what the String Constant Pool is in Java, how it optimizes memory, and why String literals are stored differently than objects. Includes code examples and explanations.
Introduction
If you’re learning Java, you’ve probably used String
values like this:
String name = "Ramesh";
But did you know this string is stored differently than using new String("Ramesh")
?
This is because of something called the String Constant Pool.
In this article, we’ll explore:
- What the String Constant Pool is
- Why Java uses it
- How it improves memory usage
- Key examples and interview tips
📌 What is the String Constant Pool?
In Java, the String Constant Pool (also called the String Intern Pool) is a special memory area in the Java Heap that stores string literals.
✅ Simply put:
It’s a memory optimization technique that avoids creating duplicate
String
objects with the same value.
🔍 How It Works
When you create a string like this:
String str1 = "Java";