Mutable vs. Immutable Data and Strings
Overview
In this lesson, students will begin to develop an understanding of the different between data that is mutable (changeable) and immutable (unchangeable).
Learning Objectives
- 1.1.B.6 Distinguish between mutable and immutable data types.
Skills
- S1.B Explain the impact design has on security.
- S1.C Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
Student Outcomes
Students will be able to:
- recognize when data would need to change during the running of a program.
- recognize when data should not change during the running of a program.
- understand waht it means for a
String
to be immutable.
Duration: 0.5 class period
Resources
Background
- 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.
When data that isn't supposed to change is stored in an immutable data type, it makes the data safer. Programs that are involved in analyzing data, should be written to store and keep those data values from changing during the analysis. That means storing the data in immutables types.
Some examples of mutable data:
- The score of a vidoe game.
- A person's mailing address.
- The ammount of money you have in a bank account.
- A person's current location as they travel to a restaurant.
- The price of a menu item in a restaurant app.
Discussion
Ask students to think about an app that they use regularly. You can ask them to use the app they used in the Determining a Data Type Lesson. Ask them: what data changes while you are using the app?
Now, let's discuss immutable data.
There are times we would not want data to change during the running of a program.
Some examples of immutable data:
- A person's social security number
- 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.
- Data related to a meteorite landing. The data it landed, the location, the size, the name of the meteorite. All of this data would not change.
- A bank account number. You can close this account, but the bank account number will stay constant. And it should. You wouldn't want a program to change the account number and you lose access to the account.
This idea of mutable and immutable data should be continuously revisited until students have a concrete understand and can make sound design decisions.
Discussion
Ask students to revisit their app from earlier. Ask them: Is there data that should not change in the app?
Strings
There are classes where the data is immutable. For example, the String
class. When you create a String
, it is immutable. Once we start working with String
data more we will see that we are unable to change the value of a String
object. We can assign a String
variable to a new String
that is an adapation of the original value.
For example, consider a database of students with information about a student: their name, address, parent contact information, course history, and grades. For this example, let's say that the name
variable holds the student's first and last name. Consider this example:
String name = "Jane Johnson";
If this student would need their name changed to something else, there is no way to modify the current value "Jane Johnson"
to be this new value. We would need to assign name
this new value. For example, maybe the student has decided to include their middle name as well, they would need to assign name
the new value, such as:
String name = "Jane Java Johnson";
As a teacher, perhaps you want to print name tags with just the student's first name. Since String
data is immutable, we are not able to modify "Jane Johnson"
to be "Jane"
. We will be able to access a part of a String
object; however in doing so, we create a new String
value that is that part of the String
. We would then assign this value to the varaible.