Mutability is just a fancy word for whether something can be changed after it is made.
Think about your favorite clay figure or a lump of dough. If you squish it, roll it, or poke a hole in it while keeping the same piece of material, that stuff is mutable. It changes its shape but stays one single object. Now imagine a Lego brick. Once it is clicked together with others to make a wall, the individual bricks themselves do not change their size or texture; only how they are arranged matters. That structure is mostly immutable because the core pieces stay fixed and solid.
The Box Analogy
To see this clearly, let us look at boxes on your desk.
Imagine you have a cardboard box with your name written on it in permanent marker. You can put socks inside one day and books the next. The box itself does not break or become a different box; you just swap what is inside. This is like a mutable list in computer code. The container stays the same, but its contents shift around.
Now look at a glass jar of pickles sealed tight with wax on top. You cannot reach inside to move the pickles without breaking the seal or changing the jar entirely. The jar and its contents are locked together as one single unit. This is like an immutable string. Once it is created, you cannot change a single letter inside it; if you want a different word, you must make a brand new jar with fresh letters.
- Mutable things: Like clay, paper notes, or a chalkboard where you can erase and write again over the same space.
- Immutable things: Like a printed book page or a photograph. The ink is set; to change it, you usually start over or paste something new on top.
When you learn programming, remembering this difference helps you decide if you want your data to stay fixed like stone (immutable) or flexible like water (mutable).
Examples
- A mutable variable is like a whiteboard you can erase and rewrite on.
- An immutable variable is like writing with permanent marker on glass.
Ask a question
See also
- What are global variables?
- What is syntax?
- What is Bob = yes?
- What is Translate high-level commands into SQL queries?
- What are unix timestamps?