Java String Constant Pool

🧠 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";

➡️ Java stores "Java" in the String Pool. If you later write:

String str2 = "Java";

➡️ Java doesn’t create a new object — it simply points str2 to the same "Java" instance in the pool.

But if you do:

String str3 = new String("Java");

➡️ It creates a new object in the heap, even if "Java" already exists in the pool.

✅ Why Does Java Do This?

  • Strings are immutable, meaning they can’t be changed once created.
  • Many programs use the same string values repeatedly (like "yes", "no", "OK").
  • So instead of creating multiple "yes" strings, Java reuses the same one in the pool, saving memory.

🔄 String Pool vs. Heap

Creation Type Stored In New Object Created?
String s = "Java" String Constant Pool ❌ No (if already exists)
String s = new String("Java") Java Heap ✅ Yes

💡 Code Example: String Pool in Action

public class StringPoolDemo {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "Hello";
        String s3 = new String("Hello");

        System.out.println(s1 == s2); // true - same reference from pool
        System.out.println(s1 == s3); // false - different objects
        System.out.println(s1.equals(s3)); // true - same value
    }
}

Output:

true
false
true

🧠 What is intern() Method?

Java provides a method called intern() to manually add a string to the pool or get the existing one.

String s4 = new String("World").intern();
String s5 = "World";

System.out.println(s4 == s5); // true

✅ Benefits of String Constant Pool

  • 💾 Memory-efficient: Reuses string objects
  • Faster comparisons: Using == for references is faster than .equals()
  • 🧘 Safe to reuse: Since strings are immutable

⚠️ Things to Remember

  • Only string literals are automatically added to the pool.
  • new String("value") always creates a new object.
  • Use intern() if you want to move a runtime string into the pool.
  • Too many string literals in large applications can still fill up memory — use wisely!

📋 Interview Tip

Q: What's the difference between == and .equals() in String comparison?

A:

  • == compares references (memory addresses)
  • .equals() compares actual values

So in String Pool:

String a = "Java";
String b = "Java";
System.out.println(a == b); // true

But with new String:

String x = new String("Java");
System.out.println(a == x); // false

✅ Summary

Concept Explanation
String Pool Special memory area to store string literals
Immutable Strings Safe to share references
"Java" vs new String("Java") First reuses, second creates a new object
intern() Manually adds a string to the pool

Final Thoughts

The String Constant Pool is a powerful concept that improves both performance and memory usage in Java applications. By understanding how it works, you can write better, more efficient Java code.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare