Quick revision
The short answer
In Java 25, primitive types in patterns, instanceof and switch are a preview feature, its third preview (JEP 507), so code using it needs --enable-preview. With it, if (value instanceof byte b) on an int value tests whether that int fits in a byte exactly, without losing information, and only then binds it to b. The feature also lets switch work on boolean, long, float and double, and it replaces manual range checks before narrowing casts.
What to remember
In this answer, we saw that primitive types in patterns, instanceof and switch are a third preview in Java 25 (JEP 507) and need --enable-preview. For a primitive value, instanceof byte b asks whether the value converts to byte exactly and binds it only then, so 100 matches and 300 does not. The feature lets switch work on boolean, long, float and double and removes manual range checks before narrowing casts. Because it is preview, it may change, so we keep it out of production code.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Java 25, primitive types in patterns, instanceof and switch are a preview feature, its third preview (JEP 507), so code using it needs --enable-preview. With it, if (value instanceof byte b) on an int value tests whether that int fits in a byte exactly, without losing information, and only then binds it to b. The feature also lets switch work on boolean, long, float and double, and it replaces manual range checks before narrowing casts.
Why this question is asked
Pattern matching in Java started with reference types: instanceof String s, case Circle c. Primitive types such as int and double were left out, so code that needs to know whether a number fits into a smaller type still uses manual range checks and casts. Interviewers ask about this feature because it tests whether we follow the language's evolution accurately, including what is final and what is still preview, and because instanceof with a primitive type means something new that is easy to misread. In this answer, we will recap preview features, look at the old way of narrowing, explain what instanceof byte b means, see the new switch forms, and finish with what preview status means for production code.
1A Quick Recap of Preview Features
A preview feature is a language feature that is fully specified and implemented but not yet permanent. It ships so that developers can try it and give feedback, and it may change or even be removed in a later release. To use one, we compile and run with --enable-preview and the matching release, for example javac --release 25 --enable-preview and java --enable-preview.
Primitive patterns were first previewed in Java 23 (JEP 455), again in Java 24 (JEP 488), and a third time in Java 25 (JEP 507). So on Java 25, the current long-term release, they are still preview.
2The Old Way: Range Checks Before a Cast
A narrowing conversion turns a wider type into a narrower one, such as int to byte. A plain cast never fails; it silently keeps the low bits, which can produce a completely different number.
The manual check works, but every narrowing needs its own range, and the check and the cast are two separate lines that can drift apart. Floating point is worse: deciding whether a double holds an exact int means comparing against limits and checking for a fractional part and for NaN.
3What instanceof byte b Means
With primitive patterns, instanceof can take a primitive type. For a primitive value, the question it asks is not about classes; it is whether the value can be converted to that type exactly, with no loss of information. If so, the pattern binds the converted value.
- Take the int valuefor example 100 or 300
- Can it convert to byte with no loss?YesBind b to the converted valueNoPattern does not match
- Run the if body with b100 -> b is 100; 300 never gets here
3 steps in order. Play them, or step through them one at a time.
Notice that the test is about the value, not about the type. 100 fits in a byte, so the pattern matches; 300 does not, so it fails, and no cast silently mangles it. The same rule gives sensible answers for the tricky floating-point conversions, where the last example shows that some longs cannot be stored exactly in a float. The JEP calls this kind of conversion exact.
4Primitive Patterns in switch
The same idea extends switch. Today a switch selector cannot be boolean, long, float or double. With the preview feature it can, and cases can use primitive type patterns with guards.
The case int i asks whether the long fits in an int, exactly as instanceof does. The final case long big is unconditional for a long selector, so the switch is exhaustive. A boolean switch with cases for true and false is exhaustive too. Because this is preview, the exact rules on dominance and exhaustiveness for these new forms could still be refined, so check the JEP for your release.
5What Preview Status Means for Us
A preview feature can change syntax or meaning between releases, and it can be withdrawn; string templates were previewed in Java 21 and 22 and then removed. Class files compiled with preview features are tied to that exact JDK feature release and will not run on others. So the practical rule is to keep primitive patterns out of production code and shared libraries for now. They are well worth trying in experiments and tests, so that we understand them when they become final, and so that our feedback can reach the JDK developers while the design is still open.
6Follow-up questions
Is value instanceof int always true when value is an int?
Yes. When the pattern type is the same as the value's type, the conversion is the identity, which is always exact, so the test is unconditional. That is also why case int i at the end of a switch on an int covers everything.
Can primitive patterns be nested in record patterns?
Yes, that is part of the motivation. With the preview enabled, a record component declared as int can be matched with a pattern such as byte b, which matches only when the component's value fits in a byte. Without the feature, nested primitive patterns must match the component's exact type.
Does this replace Math.toIntExact?
For tests, yes in spirit: value instanceof int i is a test that does not throw, while Math.toIntExact throws ArithmeticException on overflow. Once the feature is final, the pattern form will often read better when the overflow case has its own branch.
7Conclusion
In this answer, we saw that primitive types in patterns, instanceof and switch are a third preview in Java 25 (JEP 507) and need --enable-preview. For a primitive value, instanceof byte b asks whether the value converts to byte exactly and binds it only then, so 100 matches and 300 does not. The feature lets switch work on boolean, long, float and double and removes manual range checks before narrowing casts. Because it is preview, it may change, so we keep it out of production code.
Open with
In Java 25, primitive types in patterns, instanceof and switch are still a preview feature, the third preview, JEP 507, after JEP 455 in Java 23 and JEP 488 in Java 24.
Then, if they want more
So to use it I need to compile and run with --enable-preview. The idea is to extend patterns to primitive types. For a primitive value, instanceof with a primitive type does not test a class; it tests whether the value can be converted to that type exactly, without losing information.
So if value is an int, if (value instanceof byte b) is true only when the value is between minus 128 and 127, and then b holds it. For 300 it is false, whereas a plain cast would silently give 44. It works for floating point too, so 42.0 instanceof int is true but 3.5 instanceof int is false.
The same feature lets switch use primitive patterns and accept boolean, long, float and double selectors, for example case int i when i > 0 on a long. What it removes is the manual range check before a narrowing cast.
Because it is preview, the syntax and rules may still change, the way string templates were withdrawn, and preview class files only run on the same JDK release, so I would keep it out of production and try it in experiments.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 21, Java 23, Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the status of primitive types in patterns, instanceof and switch in Java 25, and...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/92cbb45e19/