Java Records, introduced in Java 14, are a new, concise way to declare reference types that are transparent holders for immutable data. They are designed to reduce boilerplate code and improve readability. In this article, we'll explore Java records with practical examples, their benefits, and how they differ from traditional classes.

What are Java Records?

Java Records are a new kind of class that provides a concise syntax for declaring immutable data carriers. They are designed to reduce boilerplate code and improve readability. Records are transparent carriers for immutable data - they don't add any functionality or behavior, just store data.
Why Use Java Records?

Java Records offer several benefits:
- They reduce boilerplate code. Records automatically generate `equals()`, `hashCode()`, and `toString()` methods based on their components.
- They improve readability. Records emphasize data over behavior, making them easier to understand.
- They encourage immutability. Records are designed to hold immutable data, which can lead to more predictable and safer code.

When to Use Java Records
Records are ideal for data carrier classes, such as DTOs (Data Transfer Objects), POJOs (Plain Old Java Objects), and value objects. They are not suitable for classes that have behavior or mutable state.
Java Record Example

Let's consider a simple example of a `Person` class before and after refactoring with a record.
Before: Traditional Class
public final class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
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);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
After: Java Record

public record Person(String name, int age) {}
As you can see, the record version is much more concise and easier to read.
Records and Components




















Records have components, which are the parameters in the record declaration. Components are public, final, and have no modifiers. Here's a table summarizing the differences:
| Traditional Class | Record |
|---|---|
| Private fields with getters | Public components (no fields) |
| Manual `equals()`, `hashCode()`, `toString()` | Automatically generated `equals()`, `hashCode()`, `toString()` |
Records and Inheritance
Records are final and cannot be subclassed. However, you can use records as supertypes in interfaces. Here's an example:
public interface User {
Person getPerson();
}
public record Employee(String name, int age, String department) implements User {}
In this example, `Employee` implements the `User` interface, which requires a `Person` record.
Records and Pattern Matching
Records work well with Java's pattern matching feature, introduced in Java 17. Here's an example:
if (person instanceof Person p) {
System.out.println("Name: " + p.name());
System.out.println("Age: " + p.age());
}
In this example, the `instanceof` operator is used with a record pattern to deconstruct the `Person` record.
Java Records are a powerful new feature that can help make your code more concise, readable, and maintainable. They are ideal for data carrier classes and encourage immutability. Give them a try in your next project!