What does it mean by String is immutable in Java?

Sharjeel Siddique
5 min readAug 30, 2022

You’ve probably heard the concept that strings are immutable in Java, which means they can’t be changed. But you may be thinking, hey, I’ve changed a string object before. All you have to do is take your variable and set it to something else., like the code below.

String name = "Ali";
name = "Zayn";

Yes, you can do that, but that’s not what string immutability means. In this article, we’ll talk about what strings being immutable actually does mean, all the reasons why they were made to be immutable, and what it means to your programs.

All right, so we know that string objects are immutable and they can’t be changed. But you know that you can declare some string and set it to some initial value. And if you want, you know, you can set it to some other value, right? So what’s the deal there? To answer that question, it’s important to know what actually happens when you create a string object.

So in our example, we create this name variable and assign it to value Ali. What Java does is create that string variable called name. But that name variable isn’t the string object itself. What it is, is a reference to a string object in memory that it creates with the value of Ali.

name points to Ali in memory.

When you take this name variable and assign it a new value in our case “Zayn”, java doesn’t actually modify this string object in memory.

What it does is create a brand new string object in memory with the value “Zayn” and change the name variable to point to that string object instead.
And, it no longer points to that “Ali” string.

when we assign to a new values, name gets dereferenced from Ali.

So when we say that string objects are immutable, we’re talking about the string object in memory. The string variables themselves can be changed to point to whatever string we want. It’s the string objects themselves that are not changeable, they are immutable.

Why does it matter? Why do we not want to be able to change these string objects? There’s actually a few great reasons. The first reason is that it enables Java to save a ton of memory space.

Let’s say we had this string name equals Ali. Then we also had string, anotherName, and it also equals Ali. These are of course, the exact same string literals, right?

String name = "Ali";
anotherName = "Ali";

When you create multiple string variables and set them all to the same literal string values, java does something pretty smart. Here is what happens under the hood.

Java creates another variable and you might think that Java is going to go ahead and create a whole new object with the value of Ali, and it’s just going to point to that, right? Well, it actually doesn’t do that. When Java creates a string object from a literal, it actually puts that string object in something called the String Pool. And then every time another string literal is created, java will check that string pool to see if that value is already anywhere in there.

both variable points to same value.

So over here, when we say anotherName equals the literal string value Ali. Java asks do I have that Ali anywhere in the pool already?

Yes, I do. And it takes that anotherName string variable and points it to the exact same string object that had already created before.

That’s pretty cool, right? It’s using half the memory that it would if it were to just create a brand new string object each time. But what does that have to do with strings being immutable?

Well, if the string objects weren’t immutable. This wouldn’t work at all! If you had both of these string variables pointing to the exact same string object in memory. If the name variable was able to change the string object in memory, like instead of saying Ali, it said Marcus. That would also change the value of the string being referenced by our anotherName variable. So this whole memory saving scheme using the string pool wouldn’t even be possible if strings were changeable.

But because strings are immutable, we don’t have to worry about any of that. We can have a million different variables all with the exact same string value and pointing to the same string object in memory. And we know that none of them are going to be disrupted because that string object and memory can never be changed. There is something to keep in mind there though. So Java will do that automatically with string literals like this

String name = "Ali;
anotherName = "Ali";

It’ll automatically use that string pool.

But if instead you go ahead and create another string variable, a third name, and instead of just assigning it to a string literal, you use the new keyword.

String name = "Ali;
anotherName = "Ali";
String thirdName = new String("Ali");

Java will go ahead and create that third name variable. But because we use the new keyword, it won’t use the shared object in the string pool. Instead, it will go ahead and create a brand new object outside of the string pool with the value of Ali. Even though it has the exact same value in it, java will go and create a whole separate object for it.

And there’s a way we can prove that too.

System.out.println(name == anotherName) // prints out true.

If we print out the value of name double equals anotherName, Java’s double equals will always return true if both of these variables are referring to the exact same object in memory. And of course we know that our name and anotherName variables should both be pointing to the exact same Ali string in the stream pool. And if we go ahead and run this, it will print out “true”.

But if instead we compared name with a third name it prints out false.

System.out.println(name == thirdName) // prints out false.

And that’s because the name and third name variables are pointing to separate strings in memory, they’re not pointing to the same objects.

In Java, Strings are also completely thread safe. So in your Java program, you could have dozens or hundreds or thousands of threads all pointing to the exact same string object in memory. And all of them can be reading that value from memory whenever they want. Even though all those threads are using it, none of them are able to change it.

If you didn’t know how Strings operate in Java language, I hope reading through this helped you gets some clarity.

--

--