Is Java String Immutable or not?
The meaning of String Immutable
Immutable means once created some an object, we cannot change modify it. Let’s talk about why does matters that String are immutable.
We create the string variable called “dog” and assign it a value,” Golden Retriever”. Now you decide to change the same variable and assign a value, “Boxer”. Before I said immutable means Once you create an object, we cannot modify it. Then how to do this? How assign value
You know already Java is an Object-Oriented Language. Then we draw some time Stack and Heap diagram for understanding some object how to create and how to assign some value. Strings store Heap Memory. That storing object is called a String pool.
Guess the value after executed code…
Yes, you already know value is “Boxer”. Now let me draw to Stack and heap diagram for above code.
Java run line by line. We break line not use enter (line break) we break by ; (semi colon).
Virtual Machine (JVM) create “Golden Retriever” object and assign ‘’dog’’ variable.
Then JVM execute line 7. Create new “Boxer” object and reference same “dog” variable.
Now think same value but different variable what happen then
Let me code it.
Now let me draw to Stack and heap diagram for above code.
Then after execute line 8 it’s not create again “Dog” object. Why?
Because String immutable. After created object we cannot change modify it. Then remember every time before create some object Java get value of that object and check the value exist in memory heap and same bit size. If it already exists and same bit size JVM not created object, Reference only map.
Now execute line 9, JVM check value same and same bit size, Yes both are same, then not created again “Dog” object. Only memory reference “pomeranianDog” to “Dog” object.
How we can proof this?
Now I want to explain Java == operator and .equals method for continue
In simply == operator compares reference or memory location of objects in a heap, .equals method compare only values in the object
Let me explain it by code.
- After execute line 11 to 14 what happened?
Can you give answer? …
It print “Yes values are same”. Because .equals method only compare value in object.
- After execute line 17 to 21 what happened?
Can you give answer? …
It print “Yes memory references are same”. Because == operator compare memory location.
“goldenDog” and “boxerDog” variable refences same “Dog” object.
- After execute line 23 to 27 what happened?
Can you give answer? …
It print “No memory references are not same”. That’s why
Let me explains this code by stack and heap diagram.
When create “Pomeranian” object it check same value object exist in heap. No any objects not same of values. Then JVM create new “Pomeranian” object and assign “pomeranian” variable.
Then there are two difference memory location. That’s why it prints “No memory references are not same”.
What do you think?
What happens when execute this code?
It print “Yes values are same”
“No memory references are not same”
What?? Why?? When execute line 8. Its same value same bit size. It says new String. Then JVM not try to check in heap for same value. It creates a new object
When execute line 11 it print memory location “1938056729”
When execute line 12 it print memory location “1938056729”
When execute line 13 it print memory location “1273765644”
When execute line 15 to 19 print “No memory references are not same”
Now you can understand well why String immutable and the benefits of it
- It makes it easier to reason about the code, since you can make assumptions about variables and arguments that you can’t otherwise make.
- It simplifies multithreaded programming since reading from a type that cannot change is always safe to do concurrently.
- It allows for a reduction of memory usage by allowing identical values to be combined together and referenced from multiple locations. Both Java and C# perform string interning to reduce the memory cost of literal strings embedded in code.
- It simplifies the design and implementation of certain algorithms (such as those employing backtracking or value-space partitioning) because previously computed state can be reused later.
- Immutability is a foundational principle in many functional programming languages – it allows code to be viewed as a series of transformations from one representation to another, rather than a sequence of mutations.
Author: Tharindu Jayathilaka.