Below are few advantages of String Immutability / Final in Java:
- Security: the system can pass on sensitive bits of read-only information without worrying that it will be altered.
- It simplifies multithreaded programming since reading from a type that cannot change is always safe to do concurrently. We can share duplicates by pointing them to a single instance.
- You can create substrings without copying. You just create a pointer into an existing base String guaranteed never to change. Immutability is the secret of Java’s very fast substring implementation.
- Immutable objects are better suited to be HashTable keys. If you change the value of an object that is used as a HashTable key without removing it and re-adding it you lose the mapping.
- Since String is immutable, inside each String is a char exactly the correct length. Unlike a StringBuilder there is no need for padding to allow for growth.
- It allows for a reduction of memory usage by allowing identical values to be combined together and referenced from multiple locations. Java performs string interning to reduce the memory cost of literal strings embedded in code.
- It simplifies the design and implementation of certain algorithms (such as those employing value-space partitioning or backtracking) because previously computed state can be reused later.