-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnull-in-switch.yaml
More file actions
54 lines (54 loc) · 1.5 KB
/
null-in-switch.yaml
File metadata and controls
54 lines (54 loc) · 1.5 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
51
52
53
54
---
id: 68
slug: "null-in-switch"
title: "Null case in switch"
category: "errors"
difficulty: "beginner"
jdkVersion: "21"
oldLabel: "Java 8"
modernLabel: "Java 21+"
oldApproach: "Guard Before Switch"
modernApproach: "case null"
oldCode: |-
// Must check before switch
if (status == null) {
return "unknown";
}
return switch (status) {
case ACTIVE -> "active";
case PAUSED -> "paused";
default -> "other";
};
modernCode: |-
return switch (status) {
case null -> "unknown";
case ACTIVE -> "active";
case PAUSED -> "paused";
default -> "other";
};
summary: "Handle null directly as a switch case — no separate guard needed."
explanation: "Pattern matching switch can match null as a case label. This eliminates\
\ the need for a null check before the switch and makes null handling explicit and\
\ visible."
whyModernWins:
- icon: "🎯"
title: "Explicit"
desc: "null handling is visible right in the switch."
- icon: "🛡️"
title: "No NPE"
desc: "Switch on a null value won't throw NullPointerException."
- icon: "📐"
title: "All-in-one"
desc: "All cases including null in a single switch expression."
support:
state: "available"
description: "Widely available since JDK 21 LTS (Sept 2023)"
prev: "errors/multi-catch"
next: "errors/record-based-errors"
related:
- "errors/helpful-npe"
- "errors/record-based-errors"
- "errors/multi-catch"
docs:
- title: "Pattern Matching for switch (JEP 441)"
href: "https://openjdk.org/jeps/441"