What is a String pool in Java
๐ก Concept: String Pool
The String pool is a special memory region in Java that stores unique String literals to optimize memory usage.
๐ Quick Intro
When a String literal is created, JVM checks the pool first to reuse existing strings instead of creating new objects.
๐ง Analogy
Think of the String pool as a library of unique books. Instead of buying multiple copies, readers share the same book.
๐ง Technical Explanation
- String literals are stored in the String pool.
- Strings created with new keyword are stored in heap separately.
- Intern() method can add strings to the pool manually.
- Helps reduce memory footprint and improve performance.
- Immutable nature of String makes sharing safe.
๐ฏ Use Cases
- โ Efficient memory usage with many duplicate strings.
- โ Performance improvements in string comparisons.
- โ Safe sharing of immutable strings across the application.
๐ป Example: String Pool Usage
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2); // true, same pool reference
System.out.println(s1 == s3); // false, s3 is new object
System.out.println(s1.equals(s3)); // true, content same

โ Interview Q&A
Q1: What is the String pool?
A: A memory region for storing unique String literals.
Q2: How are strings added to the pool?
A: Automatically for literals, manually via intern().
Q3: What is the benefit of String pool?
A: Saves memory by reusing strings.
Q4: What does intern() do?
A: Adds string to pool and returns pooled reference.
Q5: Are Strings mutable?
A: No, they are immutable.
Q6: How to compare strings correctly?
A: Use equals() method.
Q7: What happens if new String is used?
A: Creates new object outside pool.
Q8: Why use == with Strings?
A: Checks reference, usually false for new objects.
Q9: Can pool reduce GC pressure?
A: Yes, by reusing objects.
Q10: Is String pool thread-safe?
A: Yes, due to String immutability.
๐ MCQs
Q1. What is the String pool?
- Heap area
- Memory region for unique String literals
- Stack area
- Code segment
Q2. How are strings added to the pool?
- Manually always
- Automatically for literals
- Never
- Only with new keyword
Q3. Benefit of String pool?
- Slows program
- Saves memory
- Increases CPU
- None
Q4. What does intern() do?
- Removes from pool
- Adds to pool
- Deletes string
- Creates new object
Q5. Are Strings mutable?
- Yes
- No
- Sometimes
- Depends
Q6. How to compare strings?
- Use ==
- Use equals()
- Use compareTo()
- Use hashCode()
Q7. What if new String is used?
- Same pool
- New object created
- No object
- Throws error
Q8. Why use == with Strings?
- Checks content
- Checks reference
- Always true
- Never use
Q9. Can pool reduce GC pressure?
- No
- Yes
- Maybe
- Unknown
Q10. Is String pool thread-safe?
- No
- Yes
- Sometimes
- Never
๐ก Bonus Insight
The String pool is a vital JVM optimization to reduce memory consumption and improve performance in Java applications.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!