‘Stupid’ Question 30: What do we mean by immutable and mutable in OO?
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.
Last modified on 2012-08-23