string

Why is String immutable in Java?

Reasons for Immutability:

  1. String Pool: Efficient space management by sharing string literals. If strings were mutable, changing one would affect all other shared references.
  2. Security: Strings are frequently used as parameters for network connections, database URLs, and file paths. Immutability ensures these cannot be changed mid-process.
  3. Thread Safety: Immutable objects are inherently thread-safe and can be shared across threads without synchronization.
  4. Caching Hashcode: The hashcode is cached during creation, making strings ideal keys for HashMaps.
Why is String immutable in Java? | DevExCode