Strings in Java: Are They Really Immutable?

Deb Prakash Chatterjee
4 min readFeb 13, 2023

--

Photo from Unsplash

We all have heard in our coding bootcamp that, Strings in Java are immutable and you can not change an immutable object.

Is this true in every sense? We will discover this in the following article.

Question —

In Java, we can declare any string variable and initialize it with some string value. Later at any point in time, we can change that value to some other string value. So exactly how it is immutable?

Answer —

To find the answer to this question, we need to understand what is happening behind the scene. Please check the code below, and follow the article.

String s = "Hello";
s = "Hi";

In the above code snippet, we have initialized a string variable named “s” with the value “Hello”. Then right after that, we assigned “Hi” inside the variable “s”.

Here “s” is a reference to a string object created in memory. “Hello” is that string object, and “s” is pointing to that object, located in the memory.

“s” is pointing to “Hello”

Whenever you again assign a value to an already-existing string variable (reference), the currently assigned string object will NOT be changed/updated. Rather a new string object will be created in the memory and the string variable will point to that new string object.

“s” is now pointing to “Hi”

Now we have understood that any string variable is a reference to a string object in the memory, and why it is immutable.

After learning this concept, you may have these questions in your mind —

1. Then what happens to the unpointed string object?

2. Why Java allows such behaviors and does not make it mutable?

Let’s discuss the answer to these questions one by one.

Question 1 —

What happens to the string object when no string variable is pointing at it?

Answer —

The simple answer is, it is taken by the Java Garbage Collector when no string variable is referenced at it.

Question 2 —

Why Java allows such behaviors and does not make it mutable?

Answer —

To understand this answer, you need to learn about the string pool first.

A string pool is, as the name suggests, a pool of strings in the memory.

Whenever you create a string object in Java, it will look into the string pool. If a potential match (of that value) exists, then the variable will point to that matched object. Otherwise, it will create a new object in the pool, and point to it.

So, if you create two or more string variables with the value “Hello”, then all the variables will point to the same memory location, where the object is located. Multiple memory locations will NOT be used to store the same string value.

Very smart indeed !

String st = "World";
String st1 = "World";

In the above example, we have two variables “st” and “st1”, and they contain the same value “World”. As per the above theory, they both should point to the same location.

Now, what would happen if strings were mutable?

Suppose at a later point in time, I have to change the value of variable “st1” to “Mars”. As it is mutable, the value of the string object will change to “Mars”, then it will look like this.

The value of variable “st” is also changed, creating a mess. If later the value of “st” is required, then the user will get a wrong value, due to the fact that strings are mutable now.

Hence it is one of the main reasons that it is immutable.

So far, I hope you understood, how strings in Java are immutable. If you wish to read more about string in Java, and its methods, then do read this article.

If you wish to connect, then connect with me on Twitter.

See you in my next post. :)

--

--

Deb Prakash Chatterjee

Software Engineer || Technical Writer || Science Enthusiast