Java String Immutability
Java `String` objects cannot be changed after they are created. Operations that appear to modify a string instead produce a new `String` object or another result.
A Java `String` is immutable: once its character sequence has been created, that sequence cannot be altered. Methods such as `concat()`, `replace()`, and `substring()` do not modify the original string; they return a result based on it.
This means a variable holding a `String` can be reassigned, but the `String` object itself is not changed. For example, after `s = s + " world"`, the variable `s` refers to a string containing the new text, while the original string remains unchanged.
Immutability makes strings safe to share because code receiving a reference cannot modify the referenced string's contents. It also supports reliable use as keys in hash-based collections because a string's contents, and therefore its `hashCode()`, do not change after construction.
Repeated string concatenation in a loop can create many intermediate strings. For substantial mutable text construction, `StringBuilder` is commonly used instead.
Key points
- `String` objects cannot have their contents changed after creation.
- Methods that seem to modify a string return a new result rather than changing the receiver.
- A reference variable can be reassigned even though the referenced `String` is immutable.
- Immutability makes `String` suitable for sharing and stable hash-based keys.
- Use `StringBuilder` when repeatedly constructing mutable text.
Interview questions
When would you use StringBuilder instead of String?
Use `StringBuilder` when text needs to be assembled through many incremental changes, especially inside a loop. Its mutable buffer avoids creating a separate immutable string for every intermediate modification. Once construction is complete, `toString()` can produce the final `String`.
Is `final String` the same thing as an immutable String?
No. `final` applies to the reference variable: after initialization, that variable cannot be assigned a different reference. String immutability applies to the object itself, whose contents cannot be changed. A non-final variable can point to different immutable strings over time.
What does it mean that String is immutable in Java?
It means that once a `String` object has been created, its character contents cannot be changed. Methods that appear to modify a string instead return another string when a different value is needed. A variable can still be reassigned to reference that new string.
What happens when you write `s = s + "x"`?
The expression produces a string containing the previous value followed by `x`. The existing string is not modified. The assignment then makes `s` refer to the resulting string.
Why is String immutability useful for HashMap keys?
A hash-based collection relies on a key's equality and hash value remaining consistent while the key is stored. Because a `String` cannot have its contents changed, its value-based `hashCode()` does not change after creation. This helps a string remain findable in a hash-based collection after insertion.