PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchJava 21 makes two pattern-matching features permanent: record patterns and pattern matching for switch. You can use both without preview flags. Start by extracting values from a record with instanceof, then use a pattern switch when behavior depends on the type or shape of a value.
What changed in Java 21 pattern matching?
Java 21 finalized record patterns (JEP 440) and pattern matching for switch (JEP 441). Oracle lists both as permanent Java 21 language features, not preview features. That means you do not need --enable-preview to use these forms when compiling for Java 21. See Oracle’s Java Language Changes for Java SE 21 and Java Language Changes Summary.
- Record patterns test a record’s type and extract its components, including components inside nested records.
- Pattern
switchselects a branch using types and patterns, so a branch can match and unpack a record in one place.
These features do not promise faster execution. Their value is in expressing type- and data-shape-based branching directly; whether that is clearer depends on the code.
Start by matching a record with instanceof
A record pattern combines a type check with access to the record’s components. For example:
record Point(double x, double y) {}
static double distanceFromOrigin(Object value) {
if (value instanceof Point(double x, double y)) {
return Math.hypot(x, y);
}
throw new IllegalArgumentException("Expected a Point");
}
If value is a Point, the pattern matches and makes its x and y components available as local pattern variables. If it is not a Point, the body does not run. Record patterns can also be nested, and Java can infer generic type arguments for record patterns in supported pattern contexts. Oracle’s Java SE 21 guide to record patterns describes the syntax and matching behavior.
Use a pattern switch for multiple shapes
When several types need different behavior, a pattern switch can put the alternatives in one expression. This example uses a sealed hierarchy so the compiler can determine that the listed cases cover the permitted shapes:
Rank #2
sealed interface Shape permits Rectangle, Circle {}
record Rectangle(double length, double width) implements Shape {}
record Circle(double radius) implements Shape {}
static double perimeter(Shape shape) {
return switch (shape) {
case Rectangle(double length, double width) -> 2 * length + 2 * width;
case Circle(double radius) -> 2 * Math.PI * radius;
};
}
Each case both identifies the record type and extracts the values used in its calculation. A switch over a hierarchy that is not closed may need a suitable fallback case, such as default, rather than assuming the listed cases are exhaustive. Oracle’s Java SE 21 guide to pattern matching for switch explains type coverage and pattern labels.
When a pattern switch is a good fit
- Several known types or record shapes require distinct results.
- Each branch naturally produces a value, making a switch expression convenient.
- The hierarchy is closed, or you can define an appropriate fallback.
When to keep an instanceof branch
A short one-off type check may be easier to read as an if. A larger switch can become harder to follow when it combines many cases, nested data shapes, guards, and null handling. Choose the form that makes the actual branches easiest to inspect; the syntax alone does not establish a performance advantage.
Recommended Free Tools
Add a when guard after the basic patterns
A when guard adds a condition that must be true after its pattern matches. The pattern variable is in scope in the guard:
static String classify(Object value) {
return switch (value) {
case String s when s.length() == 1 -> "one character";
case String s -> "string";
case null -> "null";
default -> "other";
};
}
The first case matches strings of length one; other strings reach the following string case. The explicit case null handles a null value. Without a null branch, do not assume that a pattern will match null: record patterns do not.
Quick Recap
Best Value
Rank #4
Order cases carefully and avoid obsolete preview examples
- Order labels to avoid dominance problems. Oracle recommends putting constants before guarded patterns and guarded patterns before unguarded patterns. A guard is a condition on a pattern match; it is not interchangeable with a constant label for checking whether a later case is unreachable.
- Handle null deliberately. Add
case nullwhen the switch should take a specific path for null. A record pattern does not match null. - Check the Java version on older examples. Record patterns in enhanced
forloop headers appeared in earlier preview material but were removed from Java 21. Do not treat that form as Java 21 syntax. - Do not confuse these finalized features with preview features. Java 21 also included unrelated preview features, such as string templates and unnamed patterns or variables. Their preview status does not make record patterns or pattern
switchpreview features.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

