final vs Immutability in Java

final : In Java, final is a modifier which is used for class, method and variable also. When a variable is declared with final keyword, it’s value can’t be modified, essentially, a constant.

Immutability : In simple terms, immutability means unchanging over time or unable to be changed. In Java, we know that String objects are immutable means we cant change anything to the existing String objects.

Differences between final and immutability

  • final means that you can’t change the object’s reference to point to another reference or another object, but you can still mutate its state (using setter methods e.g). Whereas immutable means that the object’s actual value can’t be changed, but you can change its reference to another one.
  • final modifier is applicable for variable but not for objects, Whereas immutability applicable for an object but not for variables.
  • By declaring a reference variable as final, we won’t get any immutability nature, Even though reference variable is final. We can perform any type of change in the corresponding Object. But we cant perform reassignment for that variable.
  • final ensures that the address of the object remains the same whereas the Immutable suggests that we can’t change the state of the object once created.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// difference between final
// and immutability
  
class Geeks {
    public static void main(String[] args)
    {
        final StringBuffer sb = new StringBuffer("Hello");
  
        // Even though reference varibale sb is final
        // We can perform any changes
        sb.append("GFG");
  
        System.out.println(sb);
  
        // Here we will get Compile time error
        // Because reassignment is not possible for final variable
        sb = new StringBuffer("Hello World");
  
        System.out.println(sb);
    }
}

chevron_right


Output:

Geeks.java:14: error: cannot assign a value to final variable sb
        sb = new StringBuffer("Hello World");
        ^
1 error

Pictorial Representation of the above Program

final vs Immutability

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

  • Declaring reference variable as final, does not mean that the object is immutable.
  • In the next line we are performing append() operation on the created object and it is successfully changed.
  • If the object is immutable, then the above append operation can’t be done.
  • But it is executed successfully as we declare reference variable as final. final means we can’t reassign anything to that reference variable again.
  • Therefore when we try to create a new object of BufferedReader then it wont created any object by throwing an error to the console.


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator I just love Programming languages and love to know new concepts everyday,every minute,every second Here to help Other GEEKS

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vedprksh79



Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.