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, has reached the release candidate stage, having completed rampdown phases. 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 moved to the release candidate phase on February 9, according to a bulletin from Oracle’s Mark Reinhold, chief architect of the Java Platform Group. A second release candidate is due February 23. The release reached an initial rampdown phase on December 8 and a subsequent rampdown phase on January 19, with developers looking to fix bugs and possibly add late enhancements, with approvals required. The JDK is due for general availability on March 21, 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. Early-access builds of JDK 20 can be found at jdk.java.net.
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, robustness, 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
andMemoryAddress
abstractions, an enhancedMemoryLayout
hierarchy, and splitting ofMemorySession
intoArena
andSegmentScope
to promote sharing of segments across maintenance boundaries. - Virtual threads, in a second preview, 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. This feature is being reincubated. - 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 thanIncompatibleClassChangeError
if no switch label applies at runtime.
Capabilities that did not make it into JDK 20 but are still under consideration for Java include universal generics, string templates, sequenced collections, and an asynchronous stack trace VM API.
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.