Java Tutorial

Records (JAVA 14+)

Data carriers with automatic getters, hashCode, equals, and toString

What are Java Records?

Introduced as a standard feature in JAVA 16, Records are a concise and modern way to created classes that only "carry" data. We often use these for database results, API responses, or simple "Point" objects.
Prior to Records, you had to write 50+ lines for a simple data class (Fields, Constructors, Getters/Setters, equals, hashCode, and toString). With a Record, you do it in ONE line!
java
// Modern Data Class!
public record User(String name, int age) {}

User u = new User("Alpha", 25);
System.out.println(u.name()); // Auto-generated getter!
System.out.println(u.toString()); // Auto-generated toString!

How it Works: Behind the Scenes

Wait! When you create a Record, the Java compiler automatically generates:
  1. Private Final fields for every component.
  2. Public Accessors (Getters) — e.g., name() instead of getName().
  3. Canonical Constructor (To initialize all data).
  4. equals() and hashCode() (To compare records by their content!).
  5. toString() (To print the user's data cleanly).

Comparisons: Class vs. Record

FeatureStandard ClassRecord
LogicFull methods, logic, and state.Pure data carrier.
MutableCan be changed (Setters).Immutable (Cannot be changed!).
GettergetName()name()
Boilerplate50-100 lines.1 line.

Customizing Records: The Compact Constructor

You can still add validation logic to your Record using a Compact Constructor. This runs before the data is saved.
java
public record User(String name, int age) {
    public User {
        if (age < 0) throw new IllegalArgumentException("Age cannot be negative!");
    }
}

Why use Records?

  1. Readability: Focus on the data, not the boilerplate code.
  2. Safety: Immutable data prevents most multi-threading bugs!
  3. Modern: The standard way to model data in modern Java projects.

Summary

TermDescription
RecordConcise, immutable data carrier
ComponentThe data fields defined in record(x, y)
AccessorThe auto-generated getter method (x())
ImmutableData that cannot be modified once it is created
BoilerplateRepetitive, generic code (Automatically removed!)
Best ForAPI data, database records, simple points
ModernJava 14 (Preview), Java 16 (Standard)
SafetyNo risk of accidentally changing data after creation
CleanPerfectly compatible with all standard Java tools
FinalAll record fields are final (cannot be changed!)
StandardEstablished and trusted worldwide for 25+ years
IdentityRecords with the SAME data are equal (u1.equals(u2))
LocalYou can define a Record INSIDE a method!
JSONPerfect for mapping Jackson/GSON API objects
CompactAdd validation logic with public Record { ... }
InterfaceRecords CAN implement interfaces!
InheritRecords CANNOT extend other classes (They extend java.lang.Record)
Final ClassAll Records are final (no children allowed!)
PatternWorks perfectly with Pattern Matching (Java 16+)!
MemoryLow memory overhead, just like a normal object
FormatCleanest way to write a "Plain Old Java Object" (POJO)
StandardReplaces many uses of libraries like Lombok!
LogicYou can add static methods or instance methods to a Record!