Pattern Matching for switch in Java 17 (Preview)
Pattern matching for switch lets a case label test the type of a value and bind it to a variable in one step. Java 17 shipped the first preview (JEP 406). After three more previews it became a standard feature in Java 21 (JEP 441).
This tutorial explains the Java 17 preview, how to enable it, and what changed before the feature was finalised.
1. Why Is This Feature Needed?
Code that handles several types usually becomes a chain of if (x instanceof A a) ... else if (x instanceof B b) .... A switch expresses the same idea more clearly, returns a value, and, for sealed types, lets the compiler confirm that every case is handled. Before this feature, switch only worked with numbers, strings and enums.
2. Syntax and Preview Flags
return switch (value) {
case null -> "null value"; // explicit null handling
case Integer i -> "integer " + i; // type pattern
case String s -> "text " + s;
default -> "something else";
};
Because it is a preview feature in Java 17, it must be enabled when compiling and running:
javac --release 17 --enable-preview TypePatternSwitch.java
java --enable-preview TypePatternSwitch
Version note: in the Java 17 preview, extra conditions were written as guarded patterns with &&, for example case String s && s.isEmpty(). Java 19 replaced this with when, which is the form used in Java 21 and later.
3. Practical Examples
How these examples were verified: a JDK 17 was not available in our test environment, so the examples were compiled and run on JDK 21, where the feature is final. They avoid guards and use only syntax that is valid in both the Java 17 preview and Java 21.
Example 1: Type patterns and case null
// Preview in Java 17 (JEP 406): javac --release 17 --enable-preview, java --enable-preview
// Final since Java 21 (JEP 441)
public class TypePatternSwitch {
static String format(Object value) {
return switch (value) {
case null -> "null value";
case Integer i -> "integer " + i;
case Long l -> "long " + l + "L";
case Double d -> String.format("double %.2f", d);
case String s -> "text \"" + s + "\" (" + s.length() + " chars)";
default -> "unsupported " + value.getClass().getSimpleName();
};
}
public static void main(String[] args) {
Object[] values = { 42, 9_000_000_000L, 3.14159, "invoice", 'x', null };
for (Object v : values) {
System.out.println(format(v));
}
}
}
integer 42 long 9000000000L double 3.14 text "invoice" (7 chars) unsupported Character null value
Example 2: Exhaustive switch over a sealed interface
The switch has no default. The compiler knows Notification is sealed and that all three permitted records are covered.
// Preview in Java 17 (JEP 406); final since Java 21 (JEP 441)
public class SealedExhaustive {
sealed interface Notification permits Email, Sms, Push { }
record Email(String to, String subject) implements Notification { }
record Sms(String phone) implements Notification { }
record Push(String device) implements Notification { }
// All permitted subtypes are covered, so no default branch is needed
static String channel(Notification n) {
return switch (n) {
case Email e -> "email to " + e.to() + " [" + e.subject() + "]";
case Sms s -> "sms to " + s.phone();
case Push p -> "push to " + p.device();
};
}
public static void main(String[] args) {
Notification[] outbox = { new Email("asha@mail.com", "Invoice"), new Sms("98450 11111"), new Push("pixel-8") };
for (Notification n : outbox) {
System.out.println(channel(n));
}
}
}
email to asha@mail.com [Invoice] sms to 98450 11111 push to pixel-8
4. Path from Preview to Final
| Version | JEP | Status |
|---|---|---|
| Java 17 | JEP 406 | first preview: type patterns, case null, guarded patterns with && |
| Java 18 | JEP 420 | second preview |
| Java 19 | JEP 427 | third preview: when guards replace && |
| Java 20 | JEP 433 | fourth preview |
| Java 21 | JEP 441 | final, no flags needed |
5. Important Points and Best Practices
- Do not use preview features in production code on Java 17; their syntax can change, and it did.
- Without
case null, a pattern switch still throwsNullPointerExceptionfor anullselector. - Order matters:
case CharSequence csbeforecase String sis a compile error because the first label dominates the second. - For production use, prefer Java 21 or later, where the feature is final.
6. Summary
- Java 17 previewed pattern matching for
switch(JEP 406). - Case labels can test types, bind variables and handle
null. - Preview features require
--enable-previewat compile time and run time. - The feature became final in Java 21 (JEP 441), with
whenguards.
Related Tutorials
- Pattern Matching for switch (Java 21)
- Sealed Classes
- Switch Expressions
- Pattern Matching for instanceof
7. References
Frequently Asked Questions (FAQ)
Q1. Is pattern matching for switch final in Java 17?
No. It is a preview feature in Java 17 (JEP 406). It became final in Java 21 (JEP 441).
Q2. How do I enable preview features in Java 17?
Compile with javac --release 17 --enable-preview and run with java --enable-preview.
Q3. What is a guarded pattern?
A pattern with an extra condition. Java 17 wrote it as case String s && s.isEmpty(); from Java 19 onward it is written case String s when s.isEmpty().
Q4. Does a pattern switch handle null?
Only if it has a case null label. Otherwise a null selector throws NullPointerException, as with older switches.