Primitive Types in Patterns, instanceof and switch in Java 25 (Third Preview)
Pattern matching used to work only with reference types. JEP 507, in its third preview in Java 25, extends patterns, instanceof and switch to primitive types: you can switch on long, float, double and boolean, use patterns like case int c when c >= 500, and test whether a value converts to another primitive type without losing information.
This tutorial shows the new forms and the "exact conversion" rule behind them.
1. Why Extend Patterns to Primitives?
Before this feature, switch did not accept long, float, double or boolean, and patterns could not mention primitive types, so code mixed patterns for objects with ad-hoc comparisons for numbers. Checking whether a long fits in an int needed manual range checks. Primitive patterns make the language uniform and make such checks safe and readable.
2. Syntax and Preview Flags
switch (code) { // int selector
case 200 -> "OK";
case int c when c >= 500 -> "error"; // primitive type pattern + guard
case int c -> "other";
}
if (bigNumber instanceof int i) { ... } // true only if the long fits exactly in an int
switch (flag) { case true -> ...; case false -> ...; }
javac --release 25 --enable-preview PrimitiveSwitch.java && java --enable-preview PrimitiveSwitch
3. Practical Examples
Example 1: Primitive patterns in switch
// Third preview in Java 25 (JEP 507): compile and run with --enable-preview
public class PrimitiveSwitch {
static String httpStatus(int code) {
return switch (code) {
case 200 -> "OK";
case 404 -> "Not Found";
case int c when c >= 500 -> "Server error " + c; // primitive type pattern with a guard
case int c -> "Other " + c;
};
}
static String temperature(double celsius) {
return switch (celsius) { // switch on double
case double d when d < 0 -> "freezing";
case double d when d < 25 -> "mild";
case double d -> "hot";
};
}
public static void main(String[] args) {
for (int code : new int[] { 200, 404, 503, 302 }) {
System.out.println(code + " -> " + httpStatus(code));
}
for (double t : new double[] { -3.5, 18.0, 41.2 }) {
System.out.println(t + " C -> " + temperature(t));
}
boolean premium = true;
System.out.println(switch (premium) { // switch on boolean
case true -> "free shipping";
case false -> "shipping 40";
});
}
}
200 -> OK 404 -> Not Found 503 -> Server error 503 302 -> Other 302 -3.5 C -> freezing 18.0 C -> mild 41.2 C -> hot free shipping
Compile and run: javac --release 25 --enable-preview PrimitiveSwitch.java && java --enable-preview PrimitiveSwitch
Example 2: Exact conversion checks with instanceof
// Third preview in Java 25 (JEP 507): compile and run with --enable-preview
public class SafeConversion {
public static void main(String[] args) {
long[] values = { 100, 40_000, 3_000_000_000L, -128 };
for (long v : values) {
// instanceof with a primitive type: true only if the value fits without loss
if (v instanceof byte b) {
System.out.println(v + " fits in a byte (" + b + ")");
} else if (v instanceof int i) {
System.out.println(v + " fits in an int (" + i + ")");
} else {
System.out.println(v + " needs a long");
}
}
double price = 19.99;
double whole = 20.0;
System.out.println("19.99 instanceof int? " + (price instanceof int));
System.out.println("20.0 instanceof int? " + (whole instanceof int));
if (whole instanceof int rupees) {
System.out.println("exact rupees: " + rupees);
}
}
}
100 fits in a byte (100) 40000 fits in an int (40000) 3000000000 needs a long -128 fits in a byte (-128) 19.99 instanceof int? false 20.0 instanceof int? true exact rupees: 20
Compile and run: javac --release 25 --enable-preview SafeConversion.java && java --enable-preview SafeConversion
19.99 instanceof int is false because converting would lose the fraction, while 20.0 instanceof int is true. A plain cast (int) 19.99 would silently give 19.
4. The Exact Conversion Rule
5. Important Points and Best Practices
- This is a preview in Java 25 and still a preview in Java 26; do not use it in production code.
- Primitive type patterns follow the same dominance and exhaustiveness rules as reference patterns.
- Use exact-conversion checks instead of manual range tests before narrowing casts.
- Compile and run with
--enable-preview.
6. Summary
- JEP 507 is in its third preview in Java 25.
- Patterns,
instanceofandswitchnow work with primitive types. - Switch accepts
long,float,doubleandbooleanselectors. - A primitive
instanceofmatches only when the conversion is exact.
Related Tutorials
- Pattern Matching for switch (Java 21)
- Record Patterns (Java 21)
- Pattern Matching for instanceof
- Stable Values (Preview)
7. References
Frequently Asked Questions (FAQ)
Q1. Can I switch on a double in Java 25?
Yes, with preview features enabled. JEP 507 allows long, float, double and boolean selectors.
Q2. What does x instanceof int mean for a long x?
It is true if the value of x can be converted to int without losing information, and then binds the converted value.
Q3. Is this feature final?
No. It is a preview in Java 25 (third preview) and continued as a preview in Java 26.
Q4. How do I enable it?
Compile with javac --release 25 --enable-preview and run with java --enable-preview.