Close

Java 14 - Records

[Updated: Mar 20, 2020, Created: Mar 18, 2020]

Java 14 enhances the programming language with records. Records provide a compact syntax for declaring classes which are plain immutable data carriers.

Before Java 14
public class Person {
    private final String name;
    private final String gender;
    private final int age;

    public Person(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name) &&
                Objects.equals(gender, person.gender);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, gender, age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}
In Java 14
public record Person(String name, String gender, int age) {}

Records are syntactical sugar which help us to avoid writing boilerplate code when creating data carriers.

This is a preview language feature in Java 14.

Java 14 IDE support: IntelliJ IDEA 2020.1 (early access)

Example

Creating record

java-14-records-example/src/com/logicbig/example/Person.java

package com.logicbig.example;

public record Person(String name, String gender, int age) {}

Using record

java-14-records-example/src/com/logicbig/example/RecordExample.java

package com.logicbig.example;

public class RecordExample {
  public static void main(String[] args) {
      Person person = new Person("Jenny", "Female", 35);
      System.out.println(person.name());
      System.out.println(person.gender());
      System.out.println(person.age());
      System.out.println(person);
  }
}
Jenny
Female
35
Person[name=Jenny, gender=Female, age=35]

Inspecting with javap

Let's use javap on the Person.class to see what it is compiled to.

D:\java-14-records-example\out\production\java-14-records-example\com\logicbig>javap Person.class
Compiled from "Person.java"
public final class com.logicbig.example.Person extends java.lang.Record {
public com.logicbig.example.Person(java.lang.String, java.lang.String, int);
public java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String name();
public java.lang.String gender();
public int age();
}

As seen above record is compiled to a normal Java class having accessors, equals(), hashCode() and toString() methods.

Also note that above compiled class is extended by java.lang.Record which is the common base class of all Java language record classes.

Example Project

Dependencies and Technologies Used:

  • Java 14
Java 14 - Records Example Select All Download
  • java-14-records-example
    • src
      • com
        • logicbig
          • example
            • Person.java

    See Also