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.
String Pool Concept Examples
1. String Literal Stored in String Pool
String s1 = "Java";
String s2 = "Java";
Explanation
- Both literals point to same object in String Constant Pool (SCP).
- Memory is reused.
- s1 == s2 → true.
2. String Created Using new Keyword (Heap)
String s1 = new String("Java");
String s2 = new String("Java");
Explanation
- Two different objects in heap memory.
- Not stored in SCP by default.
- s1 == s2 → false.
3. Literal vs new Keyword Comparison
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Explanation
- == compares reference → false
- .equals() compares content → true
- Classic interview question
4. String Pool Reuse Demonstration
String a = "Selenium";
String b = "Selenium";
System.out.println(a == b);
Explanation
- Both refer to same pooled object.
- Output: true.
5. Heap Object with Same Content as Pool Object
String a = "Test";
String b = new String("Test");
System.out.println(a == b);
Explanation
- a → SCP
- b → Heap
- Output: false.
6. Using intern() to Move String to Pool
String s1 = new String("Java");
String s2 = s1.intern();
String s3 = "Java";
System.out.println(s2 == s3);
Explanation
- intern() returns reference from SCP.
- Output: true.
7. intern() with Existing Pool Entry
String s1 = "Automation";
String s2 = new String("Automation").intern();
System.out.println(s1 == s2);
Explanation
- SCP already contains "Automation".
- intern() points to existing pooled object.
8. String Concatenation at Compile Time
String s1 = "Java" + "Selenium";
String s2 = "JavaSelenium";
System.out.println(s1 == s2);
Explanation
- Compile-time concatenation.
- Result stored in SCP.
- Output: true.
9. String Concatenation at Runtime (Variable)
String s1 = "Java";
String s2 = "Selenium";
String s3 = s1 + s2;
String s4 = "JavaSelenium";
System.out.println(s3 == s4);
Explanation
- Runtime concatenation creates new heap object.
- Output: false.
10. Runtime Concatenation with intern()
String s1 = "Java";
String s2 = "Selenium";
String s3 = (s1 + s2).intern();
String s4 = "JavaSelenium";
System.out.println(s3 == s4);
Explanation
- intern() moves result to SCP.
- Output: true.
11. String Pool with Final Variables
final String s1 = "Java";
String s2 = s1 + "Selenium";
String s3 = "JavaSelenium";
System.out.println(s2 == s3);
Explanation
- final enables compile-time optimization.
- Stored in SCP.
- Output: true.
12. String Pool Without final
String s1 = "Java";
String s2 = s1 + "Selenium";
String s3 = "JavaSelenium";
System.out.println(s2 == s3);
Explanation
- Runtime concatenation.
- Output: false.
13. String Pool and equals()
String s1 = new String("Test");
String s2 = "Test";
System.out.println(s1.equals(s2));
Explanation
- Content comparison only.
- Output: true.
- Pool does not affect .equals().
14. Multiple Heap Objects with Same Content
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2);
Explanation
- Two separate heap objects.
- Output: false.
15. Heap Object + Pool Object Reference Count
String s1 = "World";
String s2 = new String("World");
String s3 = s2.intern();
System.out.println(s1 == s3);
Explanation
- intern() returns pool reference.
- Output: true.
16. String Pool Memory Efficiency Example
String s1 = "QA";
String s2 = "QA";
String s3 = "QA";
Explanation
- Only one object in SCP.
- All references point to same object.
17. String Pool with Different Content
String s1 = "QA";
String s2 = "Qa";
System.out.println(s1 == s2);
Explanation
- Case-sensitive.
- Two different pool entries.
- Output: false.
18. String Pool with Substring
String s1 = "Automation";
String s2 = s1.substring(0, 4);
String s3 = "Auto";
System.out.println(s2 == s3);
Explanation
- substring() creates new object.
- Output: false.
19. Substring + intern()
String s1 = "Automation";
String s2 = s1.substring(0, 4).intern();
String s3 = "Auto";
System.out.println(s2 == s3);
Explanation
- intern() forces pool usage.
- Output: true.
20. Interview Summary Example (Most Important)
String a = "Java";
String b = new String("Java");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
Explanation
- == → reference comparison
- .equals() → content comparison
- Top-priority interview question