1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: The String Pool (String Constant Pool) is a special memory area storing unique string literals. It enables memory optimization by reusing identical strings instead of creating duplicates.
Like Spotify caching album art—millions of users viewing "Thriller" all see the same cached image, not millions of copies!
2. Conceptual Clarity
Memory Layout
3. Technical Mastery
| Operation | Pool Behavior |
|---|---|
Literal "text" | Added to pool (or reuses existing) |
new String("text") | New heap object + literal in pool |
intern() | Returns pool reference; adds if absent |
| Runtime concat | Creates new heap object (not pooled) |
4. Interactive & Applied Code
public class StringPool {
public static void main(String[] args) {
// === POOL SHARING ===
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true (same pool object)
// === HEAP OBJECTS ===
String s3 = new String("Hello");
System.out.println(s1 == s3); // false (different memory)
System.out.println(s1.equals(s3)); // true (same content)
// === INTERN METHOD ===
String s4 = s3.intern(); // Get pool reference
System.out.println(s1 == s4); // true (both from pool)
// === RUNTIME CONCATENATION ===
String first = "Hello";
String second = " World";
String combined = first + second;
String literal = "Hello World";
System.out.println(combined == literal); // false (runtime concat not pooled)
System.out.println(combined.intern() == literal); // true
// === COMPILE-TIME OPTIMIZATION ===
String ct1 = "Hello" + " World"; // Compiler optimizes to literal
String ct2 = "Hello World";
System.out.println(ct1 == ct2); // true (both from pool)
// === MEMORY DEMONSTRATION ===
// Without pool: 1 million "error" strings = 1 million objects
// With pool: 1 million "error" strings = 1 object + 1 million references
// === WHEN TO USE INTERN() ===
// Reading large files with many duplicate strings
String[] largeData = new String[100000];
for (int i = 0; i < largeData.length; i++) {
// Without intern: many duplicate objects
// largeData[i] = readFromFile();
// With intern: reuse from pool
// largeData[i] = readFromFile().intern();
}
}
}⚠️ Common Mistakes
Mistake #1: Overusing intern()
// ❌ Bad: interning every string adds overhead
for (String s : data) {
s.intern(); // Pool lookup cost + GC pressure
}
// Only intern when many duplicates expectedMistake #2: Assuming runtime strings are pooled
String a = userInput; // From console
String b = "expected";
if (a == b) { } // ❌ Won't match even if same content
if (a.equals(b)) { } // ✅ Correct5. The "Interview Corner"
🏆 Q1: "Where is String Pool located?" Answer: In Heap memory (was in PermGen before Java 7). This allows garbage collection of unused pooled strings.
🏆 Q2: "How many objects: String s = new String("Hello")?"
Answer: Up to 2: One in pool (literal), one on heap (new). If "Hello" already in pool, just 1 new heap object.
🏆 Q3: "When should you use intern()?" Answer: When processing large datasets with many duplicate strings (parsing files, XML, logs). Avoid for unique strings—adds overhead.
🎓 Key Takeaways
✅ String Pool stores unique literals
✅ Literals share references (memory efficient)
✅ new String() creates separate heap objects
✅ intern() returns pool reference
✅ Pool is in Heap, GC-collected (Java 7+)