Java Snippet

Java Record for Lightweight DTOs

Difficulty: Easy

Records (Java 14 preview, stable since Java 16) collapse boilerplate immutable data carriers into one line. This snippet shows the canonical record, a compact constructor for validation, and using records as map keys. The runnable code targets Java 13 syntax (since the test runner is OpenJDK 13), with the modern record equivalent shown inline in comments and explanations.