Java records tutorial

The most significant addition to the Java language since Sun Microsystems rewrote the collections API for Java 5 is the introduction of Java records.

Java records address two significant pain points that software developers of all stripes complain about:

  1. The frustrating verbosity of even simple Java programs.
  2. The ubiquitous desire for improved runtime performance and scalability.

Java records tackle these problems by providing a simple programming construct that lets developers declare immutable data structures. The performance of these components is then highly optimized by the JVM at runtime.

What is a Java record?

A Java record is a new, top-level Java type (joining class, interface and enum) that only contains immutable data. That means the properties a record references are constant and cannot change at runtime. To put this in Java terms, all properties declared as part of a record are final.

When the Java runtime knows a set of properties will not change during the application's lifecycle, the JVM can perform a variety of optimization steps behind the scenes. This is why the use of Java records results in faster programs that can scale more linearly.

Furthermore, the syntax for declaring a Java record is delightfully simple.

Java records example

Here's an example of a Java record that would track the highs and lows of a stock on any given day.

public record PriceRange(double high, double low, LocalDate date) {}

To create a PriceRange record for a stock that traded between $12 and $10 today, the syntax would be the following.

var priceRange = new PriceRange(12, 10, LocalDate.now());

Java's canonical constructor

Keen-eyed Java developers will notice that the above example uses a parameterized constructor to create an instance of the Java record, even though no constructor is declared. That's because Java records introduce a new, implicit constructor type known as the canonical constructor.

The canonical constructor's method signature matches the variable listing used when the record is declared. Removing the need to write highly parameterized constructors is just one more way Java records reduce the software developer's programming burden.

Setters and getters in Java records

Java developers with a long history of working with JavaBeans know the displeasure of creating setters and getters for every declared instance variable. With Java records, developers are absolved of these duties for the following reasons:

  • Since all properties associated with a record are final, there's no need for setters.
  • Every property in a record has an implicit getter method that matches the property name.

For example, to print out the daily high and low of the PriceRange record instantiated above, the code would be the following.

System.out.println(priceRange.high()); // prints 12
System.out.println(priceRange.low()); // prints 10

Java records tutorial

The Java language is constantly evolving, but some advancements are more significant than others. The March 2020 inclusion of Java records is one of the most impactful and welcome changes to the Java platform in recent years.

Watch the full Java records tutorial to learn how to start integrating Java records into your code.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close