
Confusion over Immutability and final in Java
In my attempts of getting ready for technical interviews of Core Java there were several popular questions that were highlighted as ‘Toughest’ and hard to answer by many bloggers. Why ‘Java is Immutable or Final?’ is one of them. We already have a good set of articles that were written on this topic. So I am not going to reinvent the tool. But thought of adding my views on some of the misconceptions people have gone through when trying to understand and explain this.
First I shall answer this question as per my understanding towards this, so it will be much easier for me to explain the misconception that lies behind this question. Immutable and final are two different concepts that come in Java. Even if you said the reason for why String is Immutable still your answer is not enough to prove why String is made final.
Immutable — Meaning content of the object can NOT be changed or modified at any cost.
Eg. String s = new String(“ABC”); — This creates a String Object in memory
When you try to change this value by adding below line
s= “abc”; — This creates a new object with the value ‘abc’ in the memory and our object variable ‘s’ of firstly created object(‘ABC’), now points to this newly created ‘abc’ object. So ultimately ‘s’ variable points to ‘abc’ object. But ‘ABC’ object still remains in the memory without having an object variable. This methodology takes place since the immutable quality of string class. Once a value is assigned to string object, that value can not be modified. That is called Immutability.
final — is key word that gives different meanings as per the context used.
- Variables — to prevent changing the variable reference to point to another reference or a value (This does NOT mean ‘final’ variables cannot be modified. They can be modified via setter method)
- Methods — to prevent getting overridden.
- Classes — to prevent getting extended.
So with above given explanations if you look at back our question, Yes String is ‘Immutable’ and the most relevant reasoning is that string objects are created in String constant pool of heap. String constant pool catch these objects and they are in the memory(String constant pool)until they get destroyed by garbage collector. This is a must as per the memory management need of Java (If the there was another request to create another object with the same values these cached objects are referred with a new variable reference). Since String is a very special class that comes in Java, a single program may have thousands of objects from this class. Therefore in order to manage the memory, String object catching methodology via the String constant pool concepts was a Must. In order to make once created objects constant String should be immutable. Then only an object can remain with same value inside till object is destroyed. (Please note that there are other set of reasons for Strings Immutability. Please refer https://javarevisited.blogspot.com/2010/10/why-string-is-immutable-or-final-in-java.html) for some other valuable hints.
Then why String is final? Here ‘final’ term relates to the String class. If you look at the implementation of String class you can see it has been declared as a final class. Because Java designers did not want any of users to extend this String class and override its behavior.
So in brief by making the String class Immutable Java designers reached their goal of memory management by letting String constant pool to catch the string objects. Moreover they wanted to preserve that behavior(Immutability) of the String class so they made the String class final.
Note — Initializing a String variable and creating and object with final keyword is a different story.
Eg. final String s1= new String(“ABC”); — this final relates to String objects variable reference. (meaning — this variable cannot refer to any other value or a variable reference) Now if we did something like given below it gives a compile error.
s1= “ghy” — The final local variable s1 cannot be assigned. It must be blank and not using a compound assignment