Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java 21 — New Features

Data-Oriented Programming

  • Records
  • Sealed Classes
  • Pattern Matching

Virtual Threads (Project Loom)

  • Virtual Threads
Chaturmind
← Java 21 — New Features

Data-Oriented Programming

  • Records
  • Sealed Classes
  • Pattern Matching

Virtual Threads (Project Loom)

  • Virtual Threads
HomeLearnJavaJava 21 — New FeaturesData-Oriented Programming
✓ FreeBeginner· 8 min read

Records

Immutable data carriers without boilerplate — records replace POJOs in most scenarios.

Published February 15, 2025


Java Records (Java 16+)

Records are immutable data classes that eliminate boilerplate. A record automatically generates:

  • Constructor
  • equals() and hashCode()
  • toString()
  • Accessor methods (not getters — name() not getName())

Basic syntax

// Traditional class — ~30 lines
public final class Point {
    private final int x;
    private final int y;
    // constructor, getters, equals, hashCode, toString ...
}

// Record — 1 line
record Point(int x, int y) {}

Point p = new Point(3, 4);
p.x();          // 3 — accessor
p.y();          // 4
p.toString();   // Point[x=3, y=4]

Custom logic in records

record User(String name, String email) {
    // Compact constructor — runs before all components are assigned
    User {
        Objects.requireNonNull(name, "name required");
        email = email.toLowerCase(); // normalize
    }

    // Custom methods are allowed
    public boolean isPremium() {
        return email.endsWith("@company.com");
    }
}

Records in Spring Boot

Records work perfectly as DTOs and response objects:

@RestController
public class UserController {

    record UserResponse(String id, String name, String email) {}

    @GetMapping("/users/{id}")
    public UserResponse getUser(@PathVariable String id) {
        return new UserResponse(id, "Alice", "alice@example.com");
    }
}

Records in DSA (interview)

Records are great for compound keys in hash maps:

record Pair(int row, int col) {} // as a HashMap key
Map<Pair, Integer> visited = new HashMap<>();
visited.put(new Pair(0, 0), 0);

Limitations

  • Records are final — cannot extend another class (but can implement interfaces)
  • All fields are private final — no mutable state

Interview Tip

Records are the idiomatic way to create value objects in modern Java. If asked about DTO design in Spring Boot, mention records as the clean solution.

Next

Sealed Classes

AI Tutor

Lesson: Records

Quick actions

AI responses can be inaccurate. Verify critical information.