Languageeasy0-2 years

Why are strings immutable in Java, and what does that cost?

Security (a validated path or class name cannot change afterwards), thread safety without locks, safe use as map keys with a cached hash, and the string pool — which only works because a pooled string cannot be modified by one holder and seen by another. The cost is that building a string incrementally allocates a new one each step.

The lesson behind it →
More on Language