Why string is immutable in java?

Most of the programmer knows string is immutable in java and we can’t change its value within the same object. If we change its value, the JVM creates a new object of string. We have discussed it in a recent post that is the immutable string in java. But still, there is a popular question of why string is immutable in java. In java there are multiple reasons to design a string is immutable in java.

Why string is immutable in java?

String in java is a final class and immutable class, so it can’t be inherited, and we can’t alter the object. There are a number of factors that considered when the string was introduced in java. We will discuss them one by one and need the concept of memory, synchronization, data structures, etc.

1. Requirement of String Pool

In java, the String is the most widely used data structure. The string constant pool is special storage in heap memory that Caching the String literals and reusing them. Whenever we create a string, the JVM checks whether the string already exists in the string constant pool or not. If string already presented the reference of the existing string will be returned, instead of creating a new object. Let’s see how to string constant pool working in memory. Here we will create three strings with same and different reference variables.

String str1 = "JavaGoal";
String str2 = "JavaGoal";
String str3 = "JavaGoal";
why string is immutable in java

Suppose if the string is not immutable and here three reference variables pointing to the same object. If we change the string with one reference will lead to the wrong value for the other references. It creates problems and affects all other references.

2. Cache of Hashcode

As we know string objects have a large use in java. The hashcode() implementation of the string is widely used in java like HashMap, HashTable, HashSet, etc. The hashcode() method returns the value of a particular string object. As we know strings are very popular as HashMap key, The immutable string gives guarantees that their value will not be changed.It means, there is no need to calculate hashcode every time it is used.

3. Security

In java, security parameters are also represented as String like in network connections, opening files, database connection URLs, usernames, and passwords, etc. Suppose if the string is mutable then any hacker could change the referenced value break the security.

4. Synchronized

Due to the immutable nature of string, it is thread-safe. Suppose multiple threads are running but they can’t be changed because if a thread changes the value, then instead of modifying the same, a new String would be created in the String pool. This eliminates the requirements of doing synchronization.

5. Performance

As we have seen the string constant pool enhances memory storage and performance. Since String is the most widely used data structure, improving the performance

6. Classloader

The String object is also used as arguments for class loading. Due to the immutability of string the correct class is getting loaded by Classloader.

Leave a Comment