As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program.
The answer to this question is very simple.
1) Firstly due to the String pool availability in java.String pool is a space in heap in java where all the string literals are stored, now suppose if there were not such pool then for each String creation we need an brand new Object although the string literal is same, so to avoid the creation of unneccesary objects and can utilize the existing object present in String pool, String made immutable in nature. Also due to this the performace can increase due to not creation of objects which is costly both in terms of space and time.
2) To avoid possible security attacks. Suppose if String won't immutable then in the middle of the some requests, some hacker can change the String and then you know ..... . The same scenario also comes in the picture of during class initialization, now suppose string are not immutable then one can simply inject the malacious code by just replacing the value of the String and jvm will start running the malacous code, so to avoid such cases String are made immutable in nature.