Strings in Java

  • https://www.baeldung.com/java-string-immutable
  • https://www.journaldev.com/797/what-is-java-string-pool

Why String is immutable in Java?

Immutable object: object whose internal state remains constant after it has been entirely created. So we can neither update the object reference nor mutate its internal state.

String Pool: Memory region in the Heap where Strings are stored.

The key benefits of keeping this class as immutable are:

  • caching
  • synchronization
  • security
  • performance