Creating Records
Overview
In this lesson, students will create a new refernce type by creating a record.
Learning Objectives
- 1.1.B.6 Distinguish between mutable and immutable data types.
- 1.1.B.7 Create new data types for immutable data using records.
- 1.1.B.8 Create instances of records.
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.
- S2.C Analyze an algorithm and program code for correctness.
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.
- create a new type by creating a record.
- create an object of a record type.
Duration: 2 class periods
Resources
Background
One way that we can store immutable data is to use a record. A Record is a reference type that allows you to create your own immutable data type where multiple pieces of related data are stored together. We can use one variable to be assigned to a record object rather than using multiple variables.
To create a record data type, you need to use the following pattern:
When we create a record, the data values that are being grouped together are listed in parenthesis after the name of the record. Each data value in the list is separated by a comma (,
). This list is called a parameter list. A parameter list contains the data type and variable name for each input value.
Vehicle Record Example
For example, a record to represent the year, make and model of a vehicle, would look like the following:
Representing an Address
A mailing address in the United States consists of:
- a street address, which can be two lines of text,
- a city,
- a state,
- a zip code, which can be either 5 digits or 5 digit followed by a dash and 4 more digits.
All of the pieces of data are of type String
.
We could create five separate variables, like the following:
String street1;
String street2;
String city;
String state;
String zipCode;
But the better way to do this is to create a new data type using a record. The data involved in a mailing address is immutable. While the person living at the address might change, the address itself does not.
The Address record would be as follows:
record USAddress(String street1, String street2, String city, String state, String zip) {
}
Representing a Bank Account
A bank account consists of:
- Type of account (savings or checking account),
- Account Owner's Name,
- Bank Routing Number,
- Bank Account Number.
The type of account would be represented as a String
.
The account owner's name could be a String
or it could be a collection of data that represents a profile for the customer, like the customer name, address, phone number, email address, etc.
The bank routing number and bank account number could start with a 0
in some cases, so we will need to use a String
.
The bank account record would be as follows:
record BankAccount(String accountType, String ownersName, String routingNumber, String accountNumber) {
}
Representing a Restaurant Receipt
A restaurant receipt might consists of:
- The server's name,
- The food total,
- The tip amount,
- The total bill.
The server's name would be represented as a String
.
The food total, tip amount, and total bill would be represented as a double
.
The restaurant receipt record would be as follows:
record RestaurantReceipt(String serverName, double foodBill, double tipAmount, double totalBill) {
}
Declaring and Initializing a Record Object
A specific instance of a record is called an object. Recall, an object is an instance of a class. We can declare and initialize many objects or instances of a class.
When we created a String
object, we did this using a String
literal and the statement looked very similar to other variable declaration and initializations we have been doing. However, with many other objects, they must be created using the class's constructor.
A constructor is used to create an object and initialize the values for the object. A constructor always has the same name as the class.
When creating an object using a constructor, the Java keyword new
is used. Since a record is a type of class, we can create an object of a record type using the following pattern:
The values in parenthesis are called arguments. Each parameter in the parameter list will need a corresponding argument. These are the values for the parameters and essentially assigns the parameter to the value of the argument. The arguments must be of the same type as the parameter and must be listed in the same order. The arguments are separated by a comma (,
). Consider the following example of creating the object myCar
.
The arguments "2020", "Toyota", "Camry"
are being passed to the parameters String year, String make, String model
as follows:
Order of Arguments Matter
If the order of the arguments does not match the order in the parameter list, then the wrong values will be assigned. In this case, that could mean confusing data, but in cases where the types do not match, you will get an error.
Using our RestaurantReceipt record as follows:
record RestaurantReceipt(String serverName, double foodBill, double tipAmount, double totalBill) {
}
If we were to attempt to create an object of this type by writing the following:
RestaurantReceipt customer1 = new RestaurantReceipt(20.55, 4.45, 25, "Rachel");
This statement will cause an error. The value 20.55
will be converted to a String
type, but the String
"Rachel"
is unable to be converted to be of type double
.
The correct way to create customer1
is as follows:
RestaurantReceipt customer1 = new RestaurantReceipt("Rachel", 20.55, 4.45, 25);