Why String is Immutable in Java?
This article is about to Immutable String, Immutable Objects and Immutable Classes in Java.
What is Immutability?
“The meaning of immutable is, that It cannot be changed or cannot be modified. “
What does it actually mean?
The meaning of the immutable is once you create an object, that object cannot be changed or modified. In Java, all the wrapper classes are immutable like Boolean, Integer, Short, Byte and String class is immutable.
The string is one of the most used class in any programming languages likewise Java. We have a little idea about the String is an immutable. Let’s explore.
“ Once you create an object, that object cannot be changed or modified. “ Now we know that is the definition of the String immutability. The Java objects are created in the heap memory. Therefore, String objects are also created in heap memory. Therefore, Java runtime keeps a special memory area to store String in Java. It is called as String Pool.
Let’s discuss the Immutability concepts using the examples.
Example 01
According to this above Figure:01, There is a String variable called name and that is assigned to Anne. Later, the variable name “ Anne “ has been changed as “Kevin”. But really happens is the, We change the value of the variable but not changed the object. (The value of the variable has been changed but does not change the object.)
- In the first line, when we assigned Anne into the name variable, what happens is the, Java create an object in the heap memory and set it as the “Anne”. Then the name variable will be pointed to the “Anne” by the java. This is what actually happens in the line one.
- In the second line, we have assigned Kevin into the name variable. Then, Java Virtual Machine(JVM) creates a new object called “Kevin” and the name variable pointed into the “Kevin”. In here we did not change the object, But we created a new object called “Kevin” and pointed to the name variable into the “Kevin”.
Example 02
Let’s see what Java does here,
The above figure 03 has three different variables. But the value is “John”.
- The first line creates a name variable and it points to John.
2. The second line creates a person variable and it points to John.
3. The third line creates an employee variable and it points to John.
Here, we can see three variables (name, person and employee) in this example, but actually java creates one object called “John” and points all three variables into this “John” object.
Example 03
This example is very similar to the second example. But there are some differences.
The above figure 05 has three different variables. But the value is “John”.
- The first line creates a name variable and it points to John.
2. The second line creates a person variable and it points to John.
3. The third line creates an employee variable and it points to John.
In here, we can see three variables (name, person and employee) in this example, But Java creates one object called “John” and points name and person variables into this “John” object.
Get Amandi Soysa’s stories in your inbox
Join Medium for free to get updates from this writer.
What will happen in this third line? Can you guess? Let’s take a look a how it will be stored in the memory. Then you can realize what actually happened there.
It creates a new object outside the String Pool and name it as “John” and the employee variable will be pointed into this “John ” object.
Immutable Objects
- Immutable object is called as an object whose contents cannot be modified or changed. In some cases we have to create immutable objects that cannot be modified or changed, once the object is created.
- It’s objects are called as Immutable objects. Immutable objects are created from Immutable classes. According to the Java String classes are immutable and therefore, the String objects are also immutable.
Immutable Class
- The Immutable class forms the immutable objects.
Steps to create Immutable Class:-
- Class must be declared as the final.
- Make all the data fields as private and final.
- There can not keep any setter methods.
- Use getter methods for all the variables.
- Include all argument constructors.
Why Java Strings are Immutable?
- security.
- It is safe for multithreading.
- Cacheable HashCode.
- Increased performance.
- Memory efficient.
- ClassLoader.
What are the Advantages of Immutable?
The following mention advantages are relevant for the objects and classes.
- It is safe for multithreading.
- The object fields cannot be modified or changed.
- It is efficient for the most of uses.
- It saves the memory (Uses less memory).
- It is easy to use and simple to understand.
Thank you for reading this article…
References
- Why is immutability matters?






