Strings in Java are Immutable or Final- Why ?

One of the most important and interesting thing about Java Strings is that they are IMMUTABLE and I am sure that you might have encountered with this term many times while studying about the Strings. So have you ever wondered –

This topic is one of the common question in interviews also and sometimes it is also framed as Why Strings in Java are Final?

What are immutable objects?

Those objects whose values can not be changed are known as Immutable objects and Java Strings are one of the kind. You might be wondering that you might have changed the values of a string many times while coding but let me tell you my friends that every time you do some operation on the String a new String object is created, even if you are performing some basic string operation like toUpperCase(), toLowerCase() etc. let’s see an example of it –

package codingeekStringTutorials;

public class StringImmutableExample {

	public static void main(String[] args) {

		String test = "codingeek";
				.println("uppercase operation performed(Not referenced again) - " + test);

		test = test.toUpperCase();
		System.out.println("uppercase operation performed(Referenced again) - " + test);


uppercase operation performed(Not referenced again) - codingeek
uppercase operation performed(Referenced again) - CODINGEEK

So in the above example if you look at the highlighted line you can see the difference that although we have performed the same operation both times but the values were different because both the times a new object was formed and we reference to the new object only on the second time.

Well everything  in a programming language have some meanings. So lets see what is the need to make String class an Immutable Class. So main points why Strings are Immutable are listed below-

Security in MultiThreaded Environment

Due to this feature of immutability in java strings we can share the variables among various threads. This also protects from any Synchronisation issues that could be generated during processing. Moreover if String are Mutable then if one object change the value of String it would be visible to another Thread although the other thread was not expecting any change in its values. It solves a lot of incosistencies that could occur otherwise.

String Pool Facility in Java

String pool is a special memory area in the Heap in which the string that are generated using literals are stored and there is a single object for a value in the memory and all the references point to the same object.



For example – If we create 5 objects with value “Codingeek” using literals then there will be only one object in the memory and all the references would be pointing to the same object. So in any case some of the reference changes the value it should not affect others . So in this case a new object is formed and the reference is set to the new object.

Read More – What are the different ways to initialize a String in Java

Cahing HashCodes for Strings

Since Strings are widely used in any programming language and are the base for any operations and it really matters to make all the String operations lightning fast. Since Strings are immutable we are sure that their hashcode is never gonna change and hence we can cache them and this really helps in using Strings as we don’t have to call function to calculate Hashcode again and again and it becomes very useful while using it with Hashmap as in this values are saved by using their hash keys as reference.

To provide Facility for other functionalities

Now to understand this lets look at this program.

HashSet<String> set = new HashSet<String>();
set.add(new String("a"));
set.add(new String("b"));
set.add(new String("c"));

for(String a: set)
	a = "a"; //Changing all vaues to a

Now if you look at the above code( If Strings are Mutable ) the pupose of the Set is violated as in a Set we can’t have duplicated content and hence immutability of Strings also provide support to various other classes and other functionalities of java.

 To provide Security features

Immutability of Java Strings also provide security features and provide protection from a lot of security Threats which could be very common during connection creation, opening a file using its name, using database username and password, calling classes using their names. All these could provide potential threats to our system.

Suppose if Strings are mutable then we might think that the connection is made to a known machine but could be possible that someone has change the credentials and now the connection is made to some unknown machine and we are still unaware of, someone could change the class loading mechanism of our system and instead of calling a class we might call some other class. This could make our code even malicious.

So Strings in java are made in such a manner so that no one could override the default behaviour of this class and hence it is very useful concept.

  • rahul garg

    Cool stuff…..

    • hiteshgarg21

      Thank You Mr Rahul…