JDK 20: The new features in Java 20

JDK 20: The new features in Java 20

Java 20, or Java Development Kit (JDK) 20, the next planned version of standard Java, is now feature complete. The six features officially marked for the release are either in an incubation or in a preview stage. These include scoped values, record patterns, pattern matching for switch statements and expressions, a foreign function and memory API, virtual threads, and structured concurrency.

Java 20 reached an initial rampdown phase on December 8, with developers now looking to fix bugs and possibly add late enhancements, with approvals required. The JDK is due for general availability on March 3, 2023, as part of Oracle’s six-month release cadence for standard Java editions. JDK 20 would follow the September 20 release of JDK 19.

These are the six features slated for JDK 20:

  • Scoped values, an API in an incubator stage of development, enables the sharing of immutable data within and across threads. These are preferred to thread-local variables, particularly when using large numbers of virtual threads. A scoped value allows data to be shared safely and efficiently between components in a large program without resorting to method arguments. Goals include ease of use, comprehensibility, and performance.
  • Record patterns, in a second preview, enhance the Java programming language with 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 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 in JDK 19 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 since being incubated in JDK 19 is that StructuredTaskScope has been updated to support inheritance of scoped values by threads created in a task scope.
  • Pattern matching for switch statements and expressions enables the concise and safe expression of complex data-oriented queries. Previously previewed in JDK 17, JDK 18, and JDK 19, this fourth preview would enable a continued co-evolution with Record Patterns, 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.

Capabilities that did not make it into JDK 19 but are still under consideration for Java include universal generics, string templates, sequenced collections, and an asynchronous stack trace VM API. Early access builds of JDK 20 can be found at jdk.java.net.

JDK 20 is due for a second rampdown phase January 19, 2023, and two release candidates in 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