INTRODUCTION TO RECORDS

java-logoRecords are a special type of class and have been introduced since version 16 of the language. The special part of records is their state, that is, the state of their instance variables, which is immutable-readable. Once an instance of a record is created the state becomes immutable, that is, the values that make up the instance variables are readable but not rewritable. We can consider a record a type that aggregates two or more values, and that allows this aggregate of values to be moved together in a lighter, less verbose form than classes. Let’s look at its definition.

Definizione

EXAMPLE OF RECORD

Let’s look at an initial example of a record.

Esempio di record

Instances of this record will have an immutable state, that is, once created they will no longer be editable.

Istanza di un record

THE STRUCTURE OF A RECORD

The definition of a record produces a class that has a field for each parameter, they are automatically generated by the compiler as private and final. This means that the components are invisible to the outside world and once initialized cannot be changed. Let’s look at a figure.

struttura di un record

The definition of a record also generates the definition of certain access methods, which give us read access to the components without having to write them ourselves.

Metodi di accesso

Another element automatically introduced by the compiler in the definition of a record is a constructor; its job is to initialize component values at the time of creating the instance of a record. This constructor replaces the default constructor in the classes we have already learned to use.

Costruttore canonico

The compiler also automatically generates the override of three methods of the Object class, hashCode() is always part of the Object class, it is an integer generated for each individual object. Two records will have the same hashCode() if they have the same values in all their components.

Override metodi di Object

WE CREATE A RECORD

Let us see how the creation of a record takes place.

Creazione di un record

Let’s look at the output produced.

Output

Specifying only the instance name invokes the toString() method of Object redefined by the compiler.

Output

Override of the equals method redefined by the compiler. Returns true since the components are equal.

Output

The hashCode() method is the same for instance p2 and p3 since as already mentioned the integer that is generated is the same for two objects if the components have the same values.

Output

Finally we see the hashCode() for instance p1.

Output

SUPERFICIALLY UNCHANGING

We have seen that the fundamental characteristic of a record is that it is immutable. Once created, it does not allow changes in its state (value of components). In fact, the state of the constituent objects is mutable. We will use in the code example the Random class, a pseudo-random number generator. Let’s look at an example.

Classe Random

value will contain a random integer between zero and fifty. We introduce a record in MyClass.

MyClass

This code shows that the value of random. Has changed only superficially, in other words, it is the object associated with component mr1 that has changed its state. In summary, record components are immutable, however, it is possible to change the values of the objects that are associated with the components, as this activity is allowed and is called surface immutability.

Immutabilità superficiale

CANONICAL CONSTRUCTOR

We will see that it is possible to redefine the canonical constructor when we need it.

Canonical constructor

This redefined constructor changes the name to uppercase (method toUpperCase()). Let’s look at the output.

Costruttore ridefinito

NON-CANONICAL CONSTRUCTOR

Let us consider the case where we want to define non canonical constructors in records. This can be done provided that at some point in its execution each non canonical builder must call up the canonical builder. Let’s look at an example.

Non canonical constructor

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

        java -jar –enable-preview CorsoJava.jar

  • Or run the main found in the file CorsoJava.java.