Why is String immutable? What is the string pool, and how many objects does new String("abc") create?
Short answer
A String cannot be changed (immutable): every "change" creates a new string. That is why equal strings can be shared in the string pool.
3 points to say:
- Immutable means safe sharing and no locks for many threads.
new String("abc")creates at most 2 objects.s = s + "d"creates a new string, the old one is untouched.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.