Mutable vs Immutable Data
- Mutable data is data that can change during the running of the program.
- Immutable data is data that cannot change during the running of the program.
Mutable Data
Some examples of mutable data:
- Your password for your favorite app
- Your screen name for an online social media site
- Your Instagram "What's on your mind?"
- Your phone number
Immutable Data
Now, let's discuss immutable data. There are times we would not want data to change during the running of a program. When data that isn't supposed to change is stored in an immutable data type, it makes the data safer.
Some examples of immutable data:
- A physical address. This is different from an address that has been assigned to a person. A person can change their address, but that address will still exist. It will likely become someone else's new address. But the physical location will not change.
- A vehicle's VIN number, make, model, year it was built. Some data about a vehicle that would be mutable: how much gas is in the gas tank, the number of miles on the car, the owner of the vehicle, the license plate.
- The data stored in a database, for example, one that stores the data meteorite landing. It included the name of the meteorites, the date, the mass, the geolocation of the landing. None of this data can change during the analysis of the data.
There are classes where the data is immutable. For example, the String
class. When you create a String
, it is immutable. We will learn more about the String
class in section 1.1.D.6 Calling Methods of a Class.
Resources