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 a Point
A point in a two-demensional space consists of:
- an x coordinate
- a y coordinate
These pieces of data are of type double
.
We could create two separate variables, like the following:
double x;
double y;
But the better way to do this is to create a new data type using a record. The data involved in a point is immutable.
The Point
record would be as follows:
record Point(double x, double y) {
}
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:
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.
Let's try it in the Java Playground using the Point
class! Toggle the console window to Detailed Output. You will see that a Point
object has been created with [x=0.0, y=0.0]
Representing a Book
A book consists of:
- Title
- Author
- Publish date
- ISBN number
The data for a Book are all of type String
.
Let's try it in Java Playground! Toggle the console window to Detailed Output. You will see that a Book
object has been created with [title=Harry Potter and The Sorcerer's Stone, author=J. K. Rowling, publishDate=1997, isbn=0-590-35340-3]
.
Misaligned Parameters
Using our Vehicle record as follows:
record Vehicle(String year, String make, String model) {
}
If we were to attempt to create an object of this type by writing the following:
Vehicle myCar = new Vehicle("Jeep", "Renegade", 2020);
The value 2020
can not be assigned to a String
type.
Let's look at another example. If we were to attempt to create an object of this type by writing the following:
Vehicle myCar = new Vehicle("Jeep", "Renegade", "2020");
Additionally, year
would be assigned "Jeep"
, make
would be assigned "Renegade"
, and model
would be assigned "2020"
.
Modify the code to update the order of the parameters so that the object myCar
is created correctly.