Why are strings made to immutable -the Real reason SUN Microsystems decided that way ..
Why strings are immutable in java ?
1.Sunchronization-Making strings immutable makes them automatically thread safe .Because with threads you have synchronization problem of one thread is about to update and other guys reads the data before updating and results into wrong evaluation of the second thread .So , such conditions can lead to lot of problem .By making strings immutable this problem is completely resolved .
2.Security - parameters are generally of string type in java networking programming like IP address of server in the form of "localhost" .
Also connection strings in database connections are also of string type like address of your Database , name of your database , username ,password .
If strings were mutable these parameters could be changed and could lead to problems of different kinds like unauthorized access to different database in the server or database unreachable . Its better to keep it unchangeable.
3.String pool - String are stored in special memory region in the heap called string pool .Here if those two strings have the same contents like "srk". Both the strings will point to the same memory location and only one copy will be their . In this way the JVM save memory so valuable resources of memory is being saved. So if one variable changes the other will also change so ambiguity will arise , this shouldn't happen so strings are immutable.
Note- strings are not made immutable for implementing string pooling .. string were made immutable for some reason and later they decided to implement string pooling as well .
4.Map keys-String objects were made immutable for storing as a key in the map . If they weren't made immutable then the key will get changed in the for example hash table .Imagine in the phone book you added a name and number associated with it and you know that you can find that number by his name but later for some reason that name was changed in the phone book . Now you will never be able to find that persons phone number the phone book will give no result or hashtable will return null
....This is the most important and major reason for making strings immutable .
That is the reason string class overrides hashcode and equals method it also implements comparable interface by defining compare to method
The hashcode of string as a key is catched as we know it's not supposed to change so it helps with performance here at the time of creation . So it doesn't need to be calculated again so it's a good choice as a key in maps and is faster then other keys in hashmap objects.
Note- stringbuffer and stringbuilder not meant for storing in a map object as key . So , if you want mutable strings use stringbuffer and stringbuilder .They also don't have equals and hashcode method, they also do not implement comparable interface and its abstract method comparator.
5.Class loading- strings are used for class loading as a parameter so making strings mutable may result in wrong class being loaded or no class being loaded at all.