Home

immutable

Immutability is a property of data whose state cannot be modified after it is created. In programming, an immutable object cannot be altered; operations that would modify it instead return a new object with the desired changes. Immutable data structures preserve the previous versions when updated and often rely on structural sharing to minimize memory use.

Many languages provide immutable primitives or types. Strings in Java and Python are immutable; numbers and

Benefits of immutability include easier reasoning about program behavior, since values do not change unexpectedly, and

Drawbacks include potential memory and performance costs from creating new objects rather than updating existing ones.

In practice, developers choose immutability to enhance safety and predictability, while mutability remains useful for performance-critical

booleans
are
typically
immutable
as
well.
Some
languages
encourage
or
enforce
immutability,
such
as
Haskell,
Erlang,
and
Clojure,
where
immutable
data
and
pure
functions
are
central.
Other
languages
allow
immutability
by
default
for
certain
types
or
offer
read-only
views.
improved
thread
safety,
as
immutable
objects
can
be
freely
shared
without
synchronization.
Immutability
also
supports
features
like
convenient
undo/redo,
snapshots,
and
persistent
data
structures.
To
mitigate
these
costs,
languages
and
libraries
use
persistent
data
structures
and
structural
sharing,
where
unchanged
portions
of
a
structure
are
reused
between
versions.
Copy-on-write
and
related
techniques
are
also
employed
to
balance
safety
with
efficiency.
or
low-level
tasks.
Common
approaches
combine
both
paradigms,
using
builders,
copy-on-write
patterns,
or
transient
mutability
for
batch
updates
before
producing
immutable
results.