String is immutable so it can be safely shared between threads, cached/interned in the String pool, and reliably used as a key in hash‑based collections (stable hashCode). It also improves security because values like class names or file paths can’t be modified after creation.
Advanced answer
Deep dive
Expanding on the short answer — what usually matters in practice:
Explain the "why", not just the "what" (intuition + consequences).
Trade-offs: what you gain/lose (time, memory, complexity, risk).
Edge cases: empty inputs, large inputs, invalid inputs, concurrency.
Examples
A tiny example (an explanation template):
// Example: discuss trade-offs for "why-is-string-immutable-in-java?"
function explain() {
// Start from the core idea:
// For security, synchronization (thread-safety), and optimization through the String Constan
}
Common pitfalls
Too generic: no concrete trade-offs or examples.
Mixing average-case and worst-case (e.g., complexity).