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.
PreviousWhat happens if you override equals() but not hashCode()?Next What is the difference between int and Integer? Where does autoboxing bite?
Is Java pass-by-value or pass-by-reference?What happens if you override equals() but not hashCode()?What is the difference between int and Integer? Where does autoboxing bite?What changed in switch, and why should a sealed switch have no default?How are overloading and overriding resolved differently?