Why string is 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 an 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.

raja
Published on 09-Feb-2018 11:10:52
Advertisements