- khangaonkar
JAVA Record tutorial
Java record type was introduced as a preview in JDK 14 and made official in JDK 16. It is a simple feature but a really important one that should be used more often. This is short tutorial on the record type.
Why should you use record ?
One of most common patterns in JAVA to pass data between objects. A recommended practice to reduce bugs and keep programs thread safe is to keep data immutable -- meaning it should not changed after creation. Prior to JDK 14 and the record type, keeping data immutable required the programmer to follow certain rules such as making the class final, making all fields final, providing getters but setters and so on. This was cumbersome and if you forgot a rule or two, your data was no longer immutable. The record type fixes this issue and reduces the needed boiler plate code.
What is a Java record ?
A Java record is a type for defining immutable data. Think of it as a read only class. The compiler generates get methods, toString(), hashcode() , equals().
How to use it ?
public record Employee(String firstName,
String lastName,
int age,
float salary,
String address) {}
The code above defines a record. Note the use of the record keyword instead of class. The fields are defined in the constructor. Explicit field definition not needed.
Employee e = new Employee("Jon","Doe", 35, 12345.6F , "London");
System.out.println(e.firstName() + e.lastName());
System.out.println(e.firstName + e.lastName);
firstName(), lastName() are generated methods. The code creates an Employee record and prints it. All the fields are final. There are no set methods, nor can you try to assign values.
e.firstName = "hello"; // compilation error
A record is final. It cannot be sub classed. The fields are final. It is effectively immutable. Ideal for bug free programs. Ideal for concurrency.
You can however define additional constructors.
public record Employee(String firstName,
String lastName,
int age,
float salary,
String address) {
public Employee(String firstName, String lastName) {
this(firstName, lastName, 0, 0.0F,"")
}
}
You can define instance methods and static methods
public record Employee(String firstName,
String lastName,
int age,
float salary,
String address) {
public Employee(String firstName, String lastName) {
this(firstName, lastName, 0, 0.0F,"")
}
public String fullName() {
return firstName + lastName;
}
public static String hello() {
return "hello";
}
}
Record are treated as values and not references. Passed by value and compared as values.
The record has a equals method the returns true if all the fields of the compared records are equal. The hashCode method is generated as well and will generate the same value for two records when the fields are equal.
Summary
In summary the record type is a very useful way to create immutable objects and write bug free code. When you need to data as objects to other objects and between threads , always prefer record to class. This type reduces boiler plate code which is a complain against JAVA.