September Java Update: Expanding Pattern Matching with Primitive Type Patterns Across All Contexts
Although the next major Java Development Kit (JDK) update, JDK 23, is not expected to be released until September, it is already generating interest with its first announced feature: an enhancement to pattern matching. This update will build upon the advancements introduced in JDK 22, which is slated for release in March and includes notable features such as previews of structured concurrency and string templates. The introduction of these new capabilities highlights Java’s ongoing commitment to evolving its features and improving developer productivity.
JDK 23 will follow JDK 22 by a six-month interval, continuing Java’s biannual release cycle. According to the OpenJDK page for JDK 23, the primary feature set to be included is a preview of primitive types in patterns. This enhancement aims to extend the capabilities of pattern matching by allowing primitive type patterns to be used in all pattern contexts, including instanceof
and switch
statements. This addition is expected to simplify code and improve consistency by enabling more uniform data exploration across different types.
The goals of the proposed enhancement are multi-faceted. One primary objective is to enable type patterns for all types, whether they are primitive or reference types, thus providing a more cohesive and expressive pattern matching experience. By aligning type patterns with instanceof
checks and ensuring that instanceof
operations are consistent with safe casting, Java aims to reduce developer friction and eliminate inconsistencies. Additionally, the proposal seeks to allow pattern matching to incorporate primitive type patterns in both nested and top-level contexts, further enhancing the flexibility and utility of this feature.
Another significant goal is to provide constructs that mitigate the risk of information loss due to unsafe casts, improving the reliability and safety of code. The enhancement also aims to allow switch
statements to process values of any primitive type, expanding the versatility of switch cases. By addressing the limitations imposed by existing restrictions on primitive types, this update seeks to make Java more uniform and expressive, streamlining pattern matching and type-checking processes.
The motivation behind this proposal stems from the need to address several restrictions that currently impose friction on developers when using pattern matching, instanceof
, and switch
statements. By removing these restrictions, Java intends to offer a more seamless and intuitive development experience. The proposal suggests that these changes will lead to more uniform and expressive code, aligning with Java’s broader goal of enhancing developer productivity and code quality.
In addition to the pattern matching improvements, JDK 23 is expected to include other features that are being previewed in JDK 22, such as updates to the class-file API and enhancements to string templates. As Oracle continues its practice of publishing new Java versions every six months, the September release of JDK 23 will provide developers with new tools and capabilities to enhance their applications. The current release, JDK 21, which debuted last September, introduced significant features like virtual threads and a generational Z garbage collector, setting a high bar for future updates.