To answer your question, the String is immutable for various ways such as for security, the parameters are represented in the network connections as String and is also represented in usernames/passwords and URLs with database connections. This could have been changed if they were mutable. In order to make the string immutable and in successfully doing so, they are made thread safe which eventually solves the synchronization problems. However, when
the string is used for class loading as arguments, if it is mutable, then this could result in a wrong class being loaded due to the change of state in the objects which are mutable. The immutability of a String states that one cannot alter the public API usage but it can bypass the normal API by using reflection. Below is an example if the String was mutable:-
String a="stack";
System.out.println(a);//prints stack
a.setValue("overflow");
System.out.println(a);//if mutable it would print overflow