- It is mainly for security reasons. String is used as parameter in network connection, database url etc. It can be easily attacked if it is mutable
- Immutability of String solves some synchronization issues, it makes the String thread safe
- To support StringPool facility. Design Strings are created in a special memory area in java heap known as “String Intern pool”. When ever you create new string variable it search for the pool to check wether it is already exist. It exist then return reference of the existing string object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references.
- To cache the hashcode of String
- To support class loading mechanism in which String is used as arguments. String being mutable results in wrong class being loaded
- when compiler optimizes your String objects, it sees that if two objects have same value (a=”test”, and b=”test”) and thus you need only one string object