-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrecord-patterns.yaml
More file actions
48 lines (48 loc) · 1.44 KB
/
record-patterns.yaml
File metadata and controls
48 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
id: 7
slug: "record-patterns"
title: "Record patterns (destructuring)"
category: "language"
difficulty: "intermediate"
jdkVersion: "21"
oldLabel: "Java 8"
modernLabel: "Java 21+"
oldApproach: "Manual Access"
modernApproach: "Destructuring"
oldCode: |-
if (obj instanceof Point) {
Point p = (Point) obj;
int x = p.getX();
int y = p.getY();
System.out.println(x + y);
}
modernCode: |-
if (obj instanceof Point(int x, int y)) {
IO.println(x + y);
}
summary: "Destructure records directly in patterns — extract fields in one step."
explanation: "Record patterns let you decompose a record's components directly in\
\ instanceof and switch. Nested patterns are supported too, enabling deep matching\
\ without intermediate variables."
whyModernWins:
- icon: "🎯"
title: "Direct extraction"
desc: "Access record components without calling accessors manually."
- icon: "🪆"
title: "Nestable"
desc: "Patterns can nest — match inner records in a single expression."
- icon: "📏"
title: "Compact code"
desc: "Five lines become two — less ceremony, same clarity."
support:
state: "available"
description: "Widely available since JDK 21 LTS (Sept 2023)"
prev: "language/sealed-classes"
next: "language/unnamed-variables"
related:
- "language/diamond-operator"
- "language/records-for-data-classes"
- "language/unnamed-variables"
docs:
- title: "Record Patterns (JEP 440)"
href: "https://openjdk.org/jeps/440"