Lesson 24. Mutable and immutable objects

 

After reading lesson 24, you’ll be able to

  • Understand what an immutable object is
  • Understand what a mutable object is
  • Understand how objects are stored in computer memory

Consider the following scenario. You buy a house that’s just the right size for you; it’s big enough for one person to live in. Then you get married, and there’s no space for your spouse. You have two choices: build an addition to the house, or tear the whole house down and build a new larger house to hold two people. Building an addition makes more sense than demolishing a perfectly good house and making an exact copy of it just to make an addition. Now, you have a kid and decide you need more room. Again, do you build an addition or tear the house down to build a new one to hold three people? Again, it makes more sense to build an extension. As you’re adding more people to your house, it’s quicker and less costly to keep the same structure and modify it.

In some situations, it helps to be able to put your data in some sort of container so you can modify the data within the container instead of having to create a new container and put the modified data in the new one.

24.1. Immutable objects

 
 
 

24.2. The need for mutability

 
 
 

Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage