Why string objects are immutable in java?

Java 8Object Oriented ProgrammingProgramming

In general, strings are used to represent the vital details like database connection urls, username passwords etc. The immutability of strings helps to keep such details unchanged.

Similarly String is used as argument while loading the class. At that time change in the string may result in loading the wrong class.

If immutable automatically variables (strings) are thread safe.

Updated on 30-Jul-2019 22:30:21