JDK 20: The new features in Java 20

Java 20, the next planned version of standard Java, has added four more proposed features recently, although they are capabilities previously previewed or incubated in standard Java and are being previewed or incubated again. Newly proposed capabilities for Java 20, or JDK (Java Development Kit) 20, are second previews of record patterns, a foreign function and memory API, and virtual threads. Structured concurrency would be re-incubated in JDK 20.

These join a previously proposed capability, pattern matching for switch statements and expressions, slated to be previewed for the fourth time in the forthcoming Java SE (Standard Edition) release. Although OpenJDK’s JDK 20 web page still lists no features for the planned release, the OpenJDK Java Enhancement Proposal (JEP) index cites these five features:

  • Record patterns to enhance the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a declarative, powerful, and composable form of data navigation and processing. Goals of the proposal include extending pattern matching to express more sophisticated, composable data queries and not changing the syntax or semantics of type patterns. Main changes since the first preview in JDK 19 include adding support for inference of type arguments of generic record patterns, support for record patterns to appear in the header of an enhanced for statement, and removing support for named record patterns.
  • Foreign function and memory API introduces an API by which Java programs can interoperate with code and data outside of the Java runtime. The API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI (Java Native Interface). The API was previously incubated in JDK Versions 17 and 18 and previewed in JDK 19. Refinements added since JDK 19 include unification of MemorySegment and MemoryAddress abstractions, an enhanced MemoryLayout hierarchy, and splitting of MemorySession into Arena and SegmentScope to promote sharing of segments across maintenance boundaries.
  • Virtual threads are lightweight threads that reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. Minor changes since the first preview include a small number of API changes made permanent in JDK 19 and degradations to ThreadGroup, also made permanent in JDK 19.
  • Structured concurrency features an API to simplify multithreaded programming and treats multiple tasks running in different threads as a single unit of work. Error-handling and cancellation are streamlined, improving reliability and enhancing observability. The only change being incubated in JDK 19 is that StructuredTaskScope is updated to support inheritance of scoped values by threads created in a task scope.
  • Pattern matching for switch statements and expressions is viewed as a mechanism to enable concise and safe expression of complex data-oriented queries. Previously previewed in JDK 17, JDK 18, and JDK 19, the fourth preview would enable a continued co-evolution with Record Patterns, also included as a preview in JDK 19, allowing for continued refinements based on experience and feedback. The main changes in pattern matching for switch since the third preview include simplified grammar for switch labels and support for inference of type arguments for generic patterns and record patterns in switch statements and expressions. Also, an exhaustive switch over an enum class now throws MatchException rather than IncompatibleClassChangeError if no switch label applies at runtime.

Other possible features for JDK 20 include universal generics and string templates. Early access builds of JDK 20 can be found at jdk.java.net.

JDK 20 is due March 21, 2023, preceded by rampdown phases in December and January and two release candidates next February. JDK 20 is set to be a short-term feature release, with only six months of Premier-level support from Oracle. JDK 21, due in September 2023, will be a long-term support release, backed by multiple years of support.

Copyright © 2022 IDG Communications, Inc.

Leave a Reply