HuntExams Academy logo
JavaIntermediate#strings

Why are Strings immutable in Java?

Answer

Security (used as DB URLs, classloader keys), thread safety, String pool reuse to save memory, and reliable hashCode caching for HashMap keys.

Example

String a = "cat";
String b = "cat";
// both point to same object in the String pool since Strings are immutable & interned

Related Interview Questions

1
JavaIntermediate#collections

Explain the Collections framework hierarchy.

Open
2
JavaBeginner#collections#list

ArrayList vs LinkedList?

Open
3
JavaIntermediate#collections#concurrency

HashMap vs Hashtable vs ConcurrentHashMap?

Open