Mutable and Immutable Data
Learn what are mutable and immutable data.
We'll cover the following...
Mutable and immutable data
Mutable means liable to change, while immutable means unable to be changed. The term mutable data means that we have data that we can change. We know that we store data in variables and that we can change it as we please, as shown in the following code block:
x = 10y = 20x = y
Here, we first assign the value 10
to the x
variable and then assign the value 20
to the y
variable. On the last line, we change the value of x
so that it’s the same as y
, which is 20
. We could say that x
is mutable because we can change it. But is this proof that x
is mutable? In some languages, it is, but in others, this is not true at all, even if the final value in x
will always be 20
. How is it possible that x
changes its value from 10
to 20
if we cannot change it? This sounds impossible.
The answer is in the way a language treats its ...