Primitive patterns keep previewing

Java 25 Course · lesson 10 of 16 · 3 min read

A third preview, and the reason this one is harder than it looks.

Open this lesson in the learning hub

Key points

  • JEP 507 previews primitive types in patterns for the third time in Java 25.
  • The feature must define exactly when a value "fits" a narrower type, for every numeric pair.
  • It also interacts with boxing, exhaustiveness and existing switch rules - a lot of surface to get right.
  • The payoff is real: no more silent truncation from a cast, and exhaustive switches over primitives.
  • It is closely tied to Valhalla, so its final shape depends on work happening elsewhere.

Primitive patterns are still previewing because "fits without loss" has to be defined for every pair.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Java 25 Course course, and every lesson in it is listed on the Java 25 Course contents page.