What do primitive type patterns add to Java's `instanceof` and `switch` at JDK 27, and what still fails to compile without `--enable-preview`?
answer
- fifth preview, not final
- every primitive, in every pattern context
- exact conversion or no match
- long selector needs the flag too
- exact record components already final
basics
~20 sAt JDK 27, in fifth preview, type patterns, instanceof and switch accept every primitive type, matching only when a conversion loses nothing. Without --enable-preview those forms fail to compile; int constant switches and exact record components still work.
solid answer
~50 sJEP 532, the fifth preview at JDK 27 and unchanged from the fourth, allows primitive types in every pattern context. `i instanceof byte b` and `case int k` work as top-level patterns, `instanceof` takes a primitive type, a `switch` selector can be `long`, `float`, `double` or `boolean`, and a record pattern can narrow, as in `R(int a)` over a `double` component. A primitive pattern matches only when the conversion is exact, so nothing is truncated: with `i = 1000`, `i instanceof byte` is false and nothing throws. Without `--enable-preview`, each of those fails with `primitive patterns are a preview feature and are disabled by default`, even `switch (n) { case 1L -> ...; default -> ... }`. What compiles without the flag: constant cases on `int`, `char`, `byte` and `short`, reference type patterns on reference-typed values, and record components named with their exact type, as in `P(int x, int y)`.
code
java · 29 linespublic class Main {
record Reading(double value) { }
static String describe(int i) {
return i instanceof byte b ? "fits a byte: " + b : "too wide: " + i;
}
static String describe(Reading r) {
return switch (r) {
case Reading(int whole) -> "whole number " + whole; // narrows only when exact
case Reading(double d) -> "fractional " + d;
};
}
static String describe(long n) {
return switch (n) { // a long selector is preview too
case 0L -> "zero";
case long x when x < 0 -> "negative";
case long x -> "positive";
};
}
public static void main(String[] args) {
System.out.println(describe(1000)); // too wide: 1000
System.out.println(describe(new Reading(30))); // whole number 30
System.out.println(describe(new Reading(30.5))); // fractional 30.5
System.out.println(describe(-5L)); // negative
}
}go deeper
Recall that at JDK 27 this is still a preview feature, that it needs a compile-time and a run-time flag, and that a primitive pattern matches only when the value fits without loss.
Explain exactness with i instanceof byte b, list what the preview adds to instanceof and switch, and separate those from final forms, such as exact primitive components in a record pattern.
Show you know what a preview costs in practice: class files tied to one release, builds needing the flag twice, and dominance rules that changed between JDK 25 and 27.
The decision is whether any code may depend on a preview: which repositories may turn it on, how that fits the upgrade cadence, and why shared libraries should wait for the final form.
## Where it stands at JDK 27 **JEP 532** is the fifth preview of primitive types in patterns, `instanceof` and `switch`. It first appeared in JDK 23 (JEP 455), was re-previewed in 24 and 25 without change, changed in JDK 26 (JEP 530: an enhanced definition of unconditional exactness and tighter dominance checks), and JDK 27 repeats that version. A **preview feature** is fully specified and implemented yet impermanent: disabled by default, and later made permanent or removed (JEP 12). So the Java Language Specification for SE 27 still describes the language without it: a selector must be `char`, `byte`, `short`, `int` or a reference type (§14.11), and a primitive type pattern applies only at that exact type (§14.30.3). ## What it adds A **type pattern** is a type plus a variable, such as `int k`. With the preview on: - **Top-level primitive patterns:** `i instanceof byte b`, `case int k`, `case int k when k >= 100`. - **Primitive type tests:** `i instanceof byte` tests without binding a variable. - **Every primitive selector:** `switch` takes `long`, `float`, `double`, `boolean` and their boxes. On these selectors a constant must match the primitive type, so `case 1L` is fine on a `long` or `Long` and `case 1` is an error, while `byte`, `short` and `char` selectors still take `case 1`. - **Narrowing in record patterns:** `R(int a)` applies to a `double` component. - **Boxes and `Object` as candidates:** `case int k` works on `Integer` or `Object`, and `case Integer k` on an `int` selector. A `boolean` `switch` listing `true` and `false` is exhaustive, and adding `default` is then an error. A pattern can also make a wrapper `switch` exhaustive: `case int p` covers a `Byte` selector, because `byte` to `int` always fits, though a `null` selector throws `NullPointerException`. ## Exact or no match A conversion is **exact** when it loses no information for that value. A primitive pattern matches only then, and a failed match skips the branch, neither truncating the value nor throwing. No new conversions exist either: `flag instanceof char` on a `boolean` is still a compile error. | Expression | Result | Why | |---|---|---| | `i instanceof byte`, `i = 42` | `true` | 42 fits | | `i instanceof byte`, `i = 1000` | `false` | a cast would give -24 | | `i instanceof float`, `i = 16_777_217` | `false` | `float` cannot hold it | | `i instanceof double`, same `i` | `true` | exact for every `int` | | `d instanceof int`, `d = 3.5` | `false` | the fraction would go | | `d instanceof int`, `d = 30.0` | `true` | the value is whole | | `d instanceof int`, `d = -0.0` | `false` | `int` has no negative zero | | `o instanceof int`, `Object o = 42` | `true` | it holds an `Integer` | | `o instanceof long`, `Object o = 42` | `false` | a cast to `long` would throw | For an `instanceof` test between primitive types that can lose information, javac 27 emits a call into the public class `java.lang.runtime.ExactConversionsSupport`, for example `isIntToByteExact`. Its documentation fixes the edge cases: negative zero, `NaN` and infinity are inexact going to an integer type, and `NaN`, infinity and signed zero are exact going to another floating-point type. ## What fails without `--enable-preview` Each `error` row reports `primitive patterns are a preview feature and are disabled by default`. | Form | Without the flag | |---|---| | `i instanceof byte`, `i instanceof byte b`, `o instanceof int k` | error | | `case int k`, on an `int`, `Integer` or `Object` selector | error | | `case Integer k` on an `int` selector | error | | `switch` on `long`, `float`, `double` or `boolean`, even with constants only | error | | `r instanceof R(int a)` over a `double` component | error | | `switch (i) { case 0 -> ...; default -> ... }` | compiles | | `o instanceof Integer k`, `case Integer k` on `Integer` | compiles | | `R(double a)`, `R(var a)`, `P(int a, int b)`, exact component types | compiles | The last row is the trap: final Java already allows primitive types nested in a record pattern, as long as they name the component's exact type. To switch it on, compile with `javac --release 27 --enable-preview Main.java` and run with `java --enable-preview Main`, or use `java --enable-preview Main.java`. `javac --enable-preview` alone is an error, as is pairing it with `--release 26`. The class file is marked `71.65535`: without the flag, `java` refuses it with `UnsupportedClassVersionError`, and a class built by JDK 25's preview does not run on 27. ## JDK 25 against JDK 27 JDK 25's third preview (JEP 507) takes `--release 25 --enable-preview`, gates the same forms and gave the same run-time results as the table above. Dominance differs: | Selector and labels, preview on | javac 25 | javac 27 | |---|---|---| | `int`: `case int _`, then `case float _` | compiles | dominated | | `int`: `case float g`, `case 16_777_216`, `default` | compiles | dominated | | `A`: `case A _`, then `case B _`, unrelated interfaces | compiles | dominated | The last row compiles at 27 without the flag, so turning the preview on can reject code that was legal. JEP 12 adds that preview class files should not be distributed beyond the control of the developer who chose them, which keeps this to trial code for now.
- With the preview on, what happens when `Byte b` is `null` in `int z = switch (b) { case int p -> 0; };`?The `switch` is exhaustive, because `byte` to `int` is exact for every value, but `null` stays outside that coverage. A null `b` throws `NullPointerException`, as in any pattern `switch` with no `case null`.
- What happens to a class compiled with the preview when it runs on another JDK?It carries a preview class-file version, `71.65535` at JDK 27. A JVM of another release must refuse it, even with the flag: a class built with JDK 25's preview fails on 27 with `UnsupportedClassVersionError`. Each release means recompiling, and possibly changing source.
- What changed in this preview between JDK 25 and JDK 27?JDK 26's fourth preview enhanced unconditional exactness and tightened dominance, and JDK 27 repeats it. So `case int _` then `case float _` on an `int` compiles under JDK 25's preview and is reported as dominated at 27. Run-time results for the forms covered here stayed the same.
- Primitive patterns are final in Java 27, so no compiler flag is needed.
- i instanceof byte b truncates the value, the way a cast does.
- A primitive pattern that cannot convert a value throws ClassCastException.
- Any primitive type in a pattern needs the flag, including Point(int x, int y).
- A switch on int with constant cases needs --enable-preview at JDK 27.
- A long switch with only constant cases compiles without the flag, since patterns alone are preview.
- Primitive types in patterns, instanceof and switch
- not finalpre-release 23
Assumes JDK 27, where this is the fifth preview; JDK 25's third preview gates the same forms, with looser dominance checks.
context
- JEP 532: Primitive Types in Patterns, instanceof, and switch (Fifth Preview)openjdk.org
- Chapter 14. Blocks, Statements, and Patternsdocs.oracle.com
- JEP 12: Preview Featuresopenjdk.org