forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord-patterns.json
More file actions
50 lines (50 loc) · 1.68 KB
/
Copy pathrecord-patterns.json
File metadata and controls
50 lines (50 loc) · 1.68 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
49
50
{
"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) {\n Point p = (Point) obj;\n int x = p.getX();\n int y = p.getY();\n System.out.println(x + y);\n}",
"modernCode": "if (obj instanceof Point(int x, int y)) {\n System.out.println(x + y);\n}",
"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"
}
]
}