← Back to Home

String Pool Concept

The String Pool (officially called the String Constant Pool – SCP) is a special memory area inside the heap where Java stores unique string literals to optimize memory usage and performance. This concept is critical for interviews and for understanding String immutability and comparison behavior.

What Is the String Pool?

  • A special memory area inside the heap
  • Stores only one copy of each unique string literal
  • Reuses existing strings instead of creating duplicates
  • Improves memory efficiency and performance
String s1 = "Java";
String s2 = "Java";
          

Both s1 and s2 point to the same object in the String Pool.

Why Does Java Have a String Pool?

  • Strings are used very frequently
  • Prevents duplicate String objects
  • Saves memory
  • Improves comparison performance
  • Supports immutability and security

How Strings Are Stored (Two Ways)

1. String Literal (Stored in String Pool)

String s1 = "Java";
String s2 = "Java";
          
  • Only one object is created
  • Both references point to the same memory location
s1 == s2   // true
          

2. Using new Keyword (Stored in Heap)

String s3 = new String("Java");
String s4 = new String("Java");
          
  • Two different objects created in heap
  • Even though content is same, references differ
s3 == s4   // false
          

Visual Understanding (Conceptual)

String Pool (Heap)
-------------------
"Java"  <---- s1, s2
Heap (Non-Pool)
-------------------
new String("Java") <---- s3
new String("Java") <---- s4
          

== vs equals() in Context of String Pool

Using == (Reference Comparison)

String a = "Test";
String b = "Test";
String c = new String("Test");
a == b   // true (same pool reference)
a == c   // false (different memory)
          

Using equals() (Content Comparison)

a.equals(c)   // true
          

String Immutability + String Pool (Key Relationship)

String s = "Java";
s.concat(" World");
          
  • "Java" remains unchanged in the pool
  • New string "Java World" is created
  • Pool safety is maintained

Why this matters: If strings were mutable, modifying one reference would affect all pooled references.

intern() Method (Advanced / Interview Topic)

What Is intern()?

  • Forces a String object into the String Pool
  • Returns the pooled reference
String s1 = new String("Java");
String s2 = s1.intern();
String s3 = "Java";

s2 == s3   // true
          

Why intern() Matters

  • Reduces duplicate strings
  • Useful in memory-sensitive applications
  • Used by JVM internally for literals

String Pool and Compile-Time Optimization

String s1 = "Java" + "World";
String s2 = "JavaWorld";
          
  • Compiler resolves this at compile time
  • Both refer to the same pooled object
s1 == s2   // true
          

Runtime Concatenation (Not Pooled Automatically)

String a = "Java";
String b = "World";
String c = a + b;
          
  • New object created at runtime
  • Not automatically added to pool
c == "JavaWorld"   // false
          

Common Misconceptions

  • String Pool is not outside heap
  • == does not compare content
  • new String() does not use pool reference directly
  • intern() does not create new string if already present

Common Beginner Mistakes

  • Using == for content comparison
  • Overusing new String()
  • Ignoring immutability impact
  • Assuming all strings go to pool automatically

Interview-Ready Answers

Short Answer

The String Pool is a special memory area in the heap where Java stores unique string literals to optimize memory usage.

Detailed Answer

In Java, the String Constant Pool stores only one instance of each unique string literal. When multiple references use the same literal, they point to the same memory location. This improves memory efficiency, performance, and supports String immutability.

Key Takeaway

The String Pool is a core optimization mechanism in Java. Understanding how strings are stored, compared, and reused is essential for efficient coding and interview success.