Sub topic 12
Which Java Version You Need
This chapter assumes you know basic Java syntax, classes, inheritance, casts, boolean operators, and the instanceof operator. It also assumes you have seen the type pattern form:
if (value instanceof String text) {
System.out.println(text.length());
}The Java version matters because pattern matching arrived in stages. A program may compile on one version and fail on another, even when the source code is otherwise valid.
Pattern matching for `instanceof`
Pattern matching for instanceof became a permanent Java language feature in Java 16. Java 14 and Java 15 included earlier preview versions.
A preview feature is an unfinished language feature that developers can try by enabling a special compiler option. Preview syntax can change or disappear, so production code should not depend on it without a deliberate decision.
| Java version | Status of type patterns in `instanceof` |
|---|---|
| Java 14–15 | Preview feature |
| Java 16 and later | Final feature |
| Java 17 and later | Available in an LTS release |
LTS means long-term support. Organizations often prefer LTS versions because vendors provide security updates and maintenance for longer periods. Java 17 and Java 21 are important LTS versions that support type patterns without preview flags.
With Java 16 or later, this code is ordinary Java source:
public class TypePatternExample {
public static void main(String[] args) {
Object value = "hello";
if (value instanceof String text) {
System.out.println(text.toUpperCase());
}
}
}Compile and run it with a matching Java installation:
javac --release 17 TypePatternExample.java
java TypePatternExampleThe --release 17 option tells the compiler to accept only Java 17 language and library APIs. This helps prevent accidentally using a feature that the target runtime does not support.
Preview history and related features
The instanceof type pattern is separate from two later pattern-matching features:
- Record patterns became final in Java 21.
- Pattern matching for
switchbecame final in Java 21.
Java 19 and Java 20 included record patterns as preview features. Pattern matching for switch appeared in preview in Java 17 and continued through later releases before becoming final in Java 21.
This history gives a practical rule:
- Use a type pattern with
instanceofin Java 16 or later. - Use record patterns or pattern matching for
switchin Java 21 or later. - Avoid preview syntax unless your build and deployment process explicitly enables and tests it.
Record patterns and switch patterns
A record pattern extends the same idea. It tests the record type and extracts its components:
public class RecordPatternExample {
record Point(int x, int y) {}
static String describe(Object value) {
if (value instanceof Point(int x, int y)) {
return "x=" + x + ", y=" + y;
}
return "not a point";
}
public static void main(String[] args) {
System.out.println(describe(new Point(3, 4)));
}
}This example requires Java 21 or later. It will not compile as final-feature code with --release 17.
Version support is therefore part of the design. Before choosing a pattern, check the minimum Java version supported by your application, build tool, and deployment runtime.
Exercise
You maintain an application whose production runtime is Java 17. Which of these features can you use without preview options: type patterns in instanceof, record patterns, or pattern matching for switch?
Show answer
You can use type patterns in instanceof. Record patterns and pattern matching for switch became final in Java 21, so they are not available as final Java 17 features.