String: Why it is Immutable?

Why String is Immutable?
This is one of the most popular interview questions. In this blog, we are going to talk about it and find the answer. String is one of the most used Class in any Application. For storing the username, password, address, IP address etc, we need to create String objects. So it is necessary to understand why our most famous and used Class is Immutable.

First of all let’s see what Immutable means:

Meaning of Immutable:

not changing or unable to be changed

In the object-oriented world, Immutable object means that once the constructor for an object has completed its execution that instance can’t be altered.

In languages like JAVA and C# String object is immutable.

Why String is Immutable?

In Java String is a final and immutable class which makes it the most special. It cannot be inherited and once created we can not alter the object. String object is one of the most used objects in any of the programs.

Let’s talk about some points which will help in understanding why String is Immutable:

1 ClassLoader:

A string is used as an argument for class loading. Let’s imagine what will happen if String is mutable. In that case, the value of the object can be changed and wrong class can be loaded.

Immutability provides security that correct class is getting loaded by Classloader.
We want to load com.generic.class.PasswordChecker class to verify the user password but may be the referenced value can be changed to com.hacked.PasswordChacker.

2 Thread Safe:

Immutability implicitly makes String thread safe. A single string instance can be shared across threads. We don’t have to use synchronization for thread safety.

3 Security:

In class loading, we have seen how string immutability helps in loading correct class and provides security. There are other examples also where the string immutability helps in gaining security.
In a banking application the username, password, bank account details etc are passed as the string. As String is immutable it’s value can’t be changed otherwise any hacker could change the referenced value to cause security issues.

4 Perfect candidate for HashMap key:

String is mostly used Object as HashMap keys. Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects.

5 Heap Space:

String immutability helps in saving a lot of java heap space because different String variables can refer to same String object in the pool. When java sees that if two objects have the same value (a=”Generic Class”, and b=”Generic Class”) and thus you need only one string object.

Wrong Assumption:

If String is immutable why the following statement is allowed?

String a = “Generic Class”;

a = “”;

One can say in the above statements he is able to change the value of string object from “Generic Class” to “”. Then, what exactly String Immutability means?

But wait this assumption is wrong. In the above statements, you are not changing the value of String object, basically, here you are changing the reference. Previously your variable a was pointing to an object which had the value “Generic Class” .
But in the second statement a new object is created with value “” and your variable a started to pointing to this new object.


The older object still have the value “Generic Class”

We discussed some reason about why String is Immutable. Hope you have enjoyed reading it. Please like, comment and share.


Leave a Reply

Your email address will not be published. Required fields are marked *