Java Records

Profile picture for user arilio666

Java Records is a new feature introduced in Java 14 that provides a concise way to declare classes mainly used to store data. Records are similar to Java classes but have some essential differences that make them ideal for representing data.

Java records consist of one or more data fields related to the member variables in a Java class.4

It is a preview feature in Java 14, meaning we won't know when it will stay permanent.
Preview features should be enabled to use this.

Syntax:

public record RecordName(ArgumentList) {
   // Record Body
}
  • The ArgumentList is a comma-separated list of parameters that define the record's state. 
  •  Each parameter is defined as a formal constructor parameter with a type and a name.
  • The record body can contain methods, just like a regular class. However, unlike regular classes, records automatically generate a constructor, equals(), hashCode(), and toString() methods based on the record's state.

Data Carrier:

A simple POJO class we use as a data carrier may contain many boilerplate codes.

public class Person {
   private String firstName;
   private String lastName;
   private int age;
   //Default constructor
   public Person() {
   }
   // Parameterized constructor
   public Person(String firstName, String lastName, int age) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.age = age;
   }
   // Getters and Setters
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   // toString() method for debugging and logging
   @Override
   public String toString() {
       return "Person{" +
               "firstName='" + firstName + '\'' +
               ", lastName='" + lastName + '\'' +
               ", age=" + age +
               '}';
   }
}
  • Now this code has more boilerplates, such as private fields, constructors, and getter and setter methods.
  • Due to Java's verbose nature, a simple carrier class such as this becomes heavy.

Record Example:

public record User(String username, String email) {
   // Record Body
}
  • The User record has three state parameters: username, email, and birthday, all strings, and a date object, respectively. 
  • The record is immutable by default, so its state cannot be changed once a User object is created.
User user = new User("JohnWick", "JohnWick@continental.com");
  • Since records generate equals(), hashCode(), and toString() methods automatically based on their state parameters, you can easily compare and display User.
User otherUser = new User("JohnWick", "JohnWick@continental.com");
System.out.println(user.equals(otherUser)); 
System.out.println(user);
  • This code creates a new User object called otherUser with the same state parameters as User.
  • The first println() statement uses the equals() method to compare the two objects, which returns true. 
  • The second println() statement uses the toString() method to display the state of the user object in a formatted string.

Output:

true
User[username=JohnWick, email=JohnWick@continental.com]
Tags