Like a Favorite Toy
Think of your favorite toy, maybe a teddy bear that you always carry with you. No matter how many times you play with it, throw it on the floor, or even take it to school, it still looks just like it did when you first got it. That's immutability in action!
Now imagine if your teddy bear changed colors every time you dropped it, that would be mutable. But since it stays the same, it’s immutable, and that makes it extra special.
Why It Matters
When something is immutable, we know exactly what to expect from it. That helps us feel safe and confident, just like knowing your favorite toy will always be there for you, no matter what!
Examples
- A number that never changes, like the number of sides on a square
- A toy that you can't break or reshape once it's made
- Your favorite color, it stays the same even if you see other colors
Ask a question
See also
- What are tagged unions?
- What are persistent data structures?
- What are recursive calls?
- What are queues and stacks?
- What is list?