String Immutability
Below are high-frequency interview questions on String immutability, with crisp, interview-ready answers. These are commonly asked in Core Java, Collections, JVM, and performance rounds.
1️⃣ What is String immutability in Java?
Answer:
String immutability means once a String object is created, its value cannot be changed. Any modification creates a new String object.
String s = "Java";
s = s.concat(" World"); // new object created
2️⃣ Why are Strings immutable in Java?
Answer:
Strings are immutable for:
- Security (class loading, file paths, URLs)
- Thread safety (safe sharing between threads)
- String pool optimization
- Hash-based collection consistency
3️⃣ How does String immutability improve security?
Answer:
Sensitive data like file paths, database URLs, and class names cannot be modified after creation, preventing malicious changes at runtime.
4️⃣ How does immutability help in multithreading?
Answer:
Immutable objects are inherently thread-safe because:
- No synchronization required
- No race conditions
- Same object can be shared safely
5️⃣ What is the String Pool and how is it related to immutability?
Answer:
The String Pool stores unique String literals.
Immutability allows safe reuse of pooled Strings without risk of modification.
String s1 = "Java";
String s2 = "Java"; // same object from pool
6️⃣ Can String be made mutable?
Answer:
❌ No. String is declared as final and cannot be modified.
✔ Use:
- StringBuilder (not thread-safe)
- StringBuffer (thread-safe)
7️⃣ Why is String class declared as final?
Answer:
To prevent inheritance and method overriding, ensuring:
- Immutability is preserved
- Security guarantees are not broken
8️⃣ Why does String override equals() and hashCode()?
Answer:
Because String is frequently used as:
- HashMap key
- HashSet element
Immutability ensures:
- hashCode() never changes
- Hash-based collections remain consistent
9️⃣ What happens when you modify a String?
Answer:
A new String object is created in memory.
String s = "Java";
s = s + " World";
Memory:
"Java" → original
"Java World" → new object
🔟 Why is String preferred as HashMap key?
Answer:
Because String is:
- Immutable
- Has cached hashCode
- Safe for hash-based collections
✔ Prevents bucket corruption
1️⃣1️⃣ Is String truly immutable? What about reflection?
Answer:
Conceptually yes, but using reflection immutability can be broken.
// Reflection can modify String (not recommended)
⚠ Interview note: This breaks JVM guarantees and is unsafe.
1️⃣2️⃣ Difference between String and StringBuilder (immutability context)
| Aspect | String | StringBuilder |
|---|---|---|
| Mutability | Immutable | Mutable |
| Thread-safe | ✔ Yes | ❌ No |
| Performance | Slower | Faster |
| Memory | More objects | Fewer objects |
1️⃣3️⃣ Why String concatenation is slow in loops?
Answer:
Each concatenation creates a new object, leading to:
- Excessive memory usage
- Performance degradation
✔ Use StringBuilder in loops.
1️⃣4️⃣ How does immutability affect performance?
Answer:
- ✔ Positive:
- Reuse via String Pool
- Cached hashCode
- ❌ Negative:
- Multiple objects during frequent modifications
1️⃣5️⃣ Can we change String value using char[]?
Answer:
No. String internally copies the character array and does not expose mutable access.
1️⃣6️⃣ What happens to old String objects after modification?
Answer:
They become eligible for Garbage Collection if no references exist.
1️⃣7️⃣ Is StringBuilder immutable?
Answer:
❌ No. StringBuilder is mutable and not thread-safe.
1️⃣8️⃣ How does immutability help caching?
Answer:
Because value never changes:
- Safe reuse
- Cached hashCode
- Predictable behavior
1️⃣9️⃣ What is cached hashCode in String?
Answer:
String caches its hashCode after first computation because immutability guarantees the value will not change.
2️⃣0️⃣ One-line interview summary of String immutability
Answer:
String immutability ensures security, thread safety, efficient memory usage, and reliable behavior in hash-based collections.
⭐ Ultra-Short Interview Answer (When Time Is Limited)
String is immutable in Java, meaning its value cannot be changed after creation. This ensures security, thread safety, efficient string pooling, and reliable hash-based collection behavior.
Key Takeaway (Must Remember)
String immutability is a design choice, not a limitation. It enables security, performance optimizations, and correctness across the Java ecosystem.