00:00

Primitive Types in Patterns, instanceof and switch in Java 25 (Third Preview)

Last updated: · Preview in Java 25

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

Syntax.java
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 -> ...; }
Commands
javac --release 25 --enable-preview PrimitiveSwitch.java && java --enable-preview PrimitiveSwitch

3. Practical Examples

Example 1: Primitive patterns in switch

PrimitiveSwitch.java
// 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";
        });
    }
}
Output
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

SafeConversion.java
// 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);
        }
    }
}
Output
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

Primitive instanceof check A primitive instanceof tests whether the value can be converted to the target type and back without losing information; if so, it matches and binds the converted value. value 40000L fits in byte? no: range -128..127 fits in int? yes, exactly binds int i i = 40000
Figure 1: A primitive pattern matches only when the conversion is exact.

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, instanceof and switch now work with primitive types.
  • Switch accepts long, float, double and boolean selectors.
  • A primitive instanceof matches only when the conversion is exact.

Related Tutorials

7. References