Java 14 - Records

[Last Updated: Apr 21, 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) { = name;
        this.gender = gender;
        this.age = age;

    public String getName() {
        return name;

    public String getGender() {
        return gender;

    public int getAge() {
        return age;

    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, &&
                Objects.equals(gender, person.gender);

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

    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)


Creating record


package com.logicbig.example;

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

Using record


package com.logicbig.example;

public class RecordExample {
  public static void main(String[] args) {
      Person person = new 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 ""
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 a constructor, 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.

More information can be found here.

Example Project

Dependencies and Technologies Used:

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

    See Also