Aug 232012
 August 23, 2012  Posted by at 11:41 pm Not So Stupid Questions  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

Oki, English is not my first language so I tend to look up English words a lot, just to make sure that I know what the word means. Immutable and mutable are words often used in OO programming, and I needed a good definition and examples as I was trying to explain this to a friend. Oki, the picture examples below are not perfect, but I couldn’t think of anything better. If you have a great example, please share! I’ll update the post and add new images 🙂

(This is NOT me) If you get a Tattoo that is finished (not a work in progress type) it is immutable, you can't change the lines- once you add a line it is a new tattoo.

A drawing of a flower on the other hand is mutable, you can erease, change, add and so on.

Immutable: The state of the object cannot be changed after creation
Example: String , you can’t change it after it is created. It looks like you can, but what happens is that a new string is created.

Mutable: The state of the object can be changed after creation
Example: Stringbuilder , you can change this object after you have created it. You can append, remove, replace, or insert characters to the same object,- a new one isn’t created as with String.

  13 Responses to “‘Stupid’ Question 30: What do we mean by immutable and mutable in OO?”

  1. I think you have your tattoo metaphor backwards. If the tattoo is finished, it’s immutable. The drawing, since it can be erased and edited in place, is mutable.

  2. immutable = unchangeable
    mutable = can change and i know cos i was just watching pluralsight videos 🙂

    • Which video? I might need to see it ha ha 😀

      • u missed one? gasp, get out of town! I actually remember it because the guy had a huge pile of dough and a bunch of cookie cutters and was putting stuff on some sort of “heap” …. dunno sounds messy 🙂

  3. You might also be interested in looking at persistent data structures[1].

    A tattoo is a good analogy for an immutable type but is also works well for persistent data structures.

    Any part of the tattoo that is drawn is immutable and cannot be changed but new tattoos can be built on top of it (eg. filling in the colour). The old tattoo, the outline, then exists both as a tattoo on it’s own and as part of a newer, bigger tattoo.

    Maybe that analogy is a little shaky, the linked list example in the wikipedia article makes things much clearer.

  4. nice way of presenting the concept…

  5. So… we know the difference. But what makes knowing this difference powerful to us as a developer?
    Large immutable objects “may” be a potential source of performance issues if they have to change regularly, but with modern PCs being so powerful would we ever see an issue? Would we have more reason to worry about this in the server space where there may be many more instances to consider?

  6. It also helps to think that the word stems from mutate/mutation. If the characters of X-men were ” immutants”, they would have a problem 🙂

  7. […] ‘Stupid’ Question 30: What do we mean by immutable and mutable in OO? and Free ‘book’ : User Experience Guidelines for Metro/Windows 8 style apps (WinRT apps) and ‘Stupid’ Question 31: What’s the deal with String and string in C#? (Iris Classon) […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 6 + 5 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)