Why string is immutable in java?

devquora
devquora

Posted On: Mar 17, 2020

 

The string is Immutable in Java language for certain reasons. One of the reasons for doing so is due to the fact that every string object is cached in the String pool. There is a risk associated because of multi-sharing of string literals among many clients as an action of a particular client affect other clients. To reduce this risk of compromising string class and to optimize performance, a string has to be immuted in java. Because of the importance and popularity of strings just like the HashMap, it is vital for the strings to be immutable as a mutable string can give two distinct hash codes.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Core Java interview questions

    Explain the significance of class loaders in Bootstrap?

    In order to load the Java Classes into a virtual environment, Classloader are used. A class loader will load key classe..

    Core Java interview questions

    What is the difference between JDK, JRE, and JVM?

    It is important to understand the difference between JDK, JRE, and JVM in Java.JVM (Java Virtual Machine) Java..

    Core Java interview questions

    What are the various access specifiers in Java?

    Access specifiers in java are the keywords which define the access scope of the function. It can be used before a class..