What is the difference between final and immutable in Java?

Final: The Final in Java is a keyword that is applicable for only variables, methods, and class. The Java Final Keyword is used to restrict the user to use variables, methods, and class. The Final keyword is applicable for all the types of variables such as for instance variable, static variable, and local variable. When a variable declared as final and assigned a value, you can't change the value of the variable. The final method can't be overridden by the child class. A class that is declared final cannot be extended. 

immutability: Immutability means once we create an object we are not allowed to change the content of that object. If any person trying to change the content if there is a change in the content with those changes a new object will be created. If there are no changes in the content existing object will be reused. 



Final vs immutable in Java: 

1. Final means that you can't change the object reference to point to another reference or another object, but you can still mutate its state (by using the setter method). Where immutable means that the object's actual value can't be changed, but you can change its reference to another one.

2. The modifier final is applicable for variables not for objects, Whereas immutability applicable for objects not for variables.

3. Final ensures that the address of the object remains the same, Whereas the immutable suggest that we can't change the state of the object once created. 

4. By declaring the reference variable as final we are not going to get any immutability nature but we can perform any type of changes in the corresponding objects and we can't reassign reference variable to any new object


Java program to demonstrate the difference between final and immutability:

class Test{
 public static void main(String args[]){
  final StringBuffer sb = new StringBuffer("Java");
  
  //Even though reference variable is final
  //we can perform any changes to corresponding object.
  sb.append("studypoint");
  System.out.println(sb);
  
  //Here, we will get compile time error because
  //we can't reassign reference variable to any new object.
  sb = new StringBuffer("Hello Javastudypoint");
  System.out.println(sb);
 }
}

Output:
What is the difference between final and immutable in Java?


Diagrammatically representation of the above program.

What is the difference between final and immutable in Java?



Explanation: In the above diagram, we can see that we are creating an object of StringBuffer class by making the reference variable as final.

1. By declaring a reference variable final, it does not mean that the object is immutable in nature.
2. In the second line, we are performing append() operation on the created object and it is successfully changed.
3. If the object is immutable, the above operation can't be done.
4. But it is executed successfully as we declare reference variable as final, which means we can't reassign anything to that reference variable again.
5. Therefore when we try to create a new object of StringBuffer class then it won't be created and we will get a compile-time error.

Reference: final vs immutability in Java.



You may also like these posts:

1. How to compare two strings lexicographically in Java?

2. How to remove leading and trailing whitespaces from a string?
3. How to convert a string into a char array?
4. Types of operators in Java with example.
5. Java OOPs Concepts with Example.
6. How to replace a string in Java?

What is the difference between final and immutable in Java? What is the difference between final and immutable in Java? Reviewed by Prashant Srivastava on January 28, 2020 Rating: 5

2 comments:

  1. Good to know that works too! I learn so much from you as well! Keep it up great post.

    ReplyDelete
  2. It was perfect the first time. I learn so much from you as well! Keep it up great post.

    ReplyDelete

Powered by Blogger.