The Immutability Concept and why it is so important

Saloni Kaur
3 min readOct 19, 2020

Every now and then you will hear developers talk about embracing immutability but what’s the real deal with it.

This article is inspired by Venkat Subramaniam’s Voxxed Days Talk. It was very well executed and here are my notes from it.

What is Immutability?

Opposite of mutability and that means modifying the state (content) of an object, whether primitive or object. And yes, we do do this very often. So what’s wrong? Let’s consider the following:

This interface has a method that returns void, and has no arguments. What this means is that the Implementer class of this interface will have to access some state exposed somewhere and probably change that (“do Something”).

This is referred to as shared mutability.

Shared mutability makes concurrency in code really tough.

Immutability in Java

Final variables, prevent modifying things..etc But is it the only way to do it?

Recursion

Recursion is a way to change the content of “value” on the stack without mutating the object.

--

--