JDK 21: The new features in Java 21

The next long-term support release of standard Java, due this fall, is set to include sequenced collections, virtual threads, string templates, and a foreign function and memory API, the latter two features in preview.

Java 21 is brewing
jazz3311 / Shutterstock

Java Development Kit (JDK) 21, due in September as the next long-term support release of Oracle’s standard Java implementation, is taking shape, with sequenced collections, virtual threads, a preview of string templates, and a third preview of a foreign function and memory API slated for the release so far.

The foreign function and memory API preview was added within the past few days, although it has yet to be officially listed on the JDK 21 release page at openjdk.org.

The specific proposals for JDK 21 so far include:

  • The foreign function and memory API enables Java programs to interoperate with code and data outside the Java runtime. By efficiently invoking foreign functions and safely accessing foreign memory, this preview API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI (Java Native Interface). The API previously was previewed in JDK 20, which debuted last month, and JDK 19, which was released in September 2022. Refinements in the latest preview include enhanced layout paths with a new element to dereference address layouts, centralized management of the lifetimes of native segments in the Arena interface, a fallback native linker implementation, and removal of the VaList. Goals of the proposal include ease of use, performance, generality, and safety. It is not a goal to either reimplement JNI on top of this API or change JNI in any way.
  • Virtual threads are lightweight threads that promise to “dramatically” reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. Goals of the plan include enabling server applications written in the simple thread-per-request style to scale with near-optimal hardware utilization, enabling existing code that uses the lang.Thread API to adopt virtual threads with minimal change, and enabling easy debugging and profiling of virtual threads with current JDK tools. Previously previewed in both JDK 20 and JDK 19, virtual threads will be finalized in JDK 21. With JDK 21, virtual threads now support thread-local variables all of the time, and make it impossible to create virtual threads that do not have these variables. Guaranteed support for thread-local variables ensures that more existing libraries can be used unchanged with virtual threads and assists with migrating task-oriented code to use virtual threads.
  • Sequenced collections introduces interfaces to represent collections with a defined encounter order. Each collection has well-defined first and second elements and so forth, to the last element. Uniform APIs are provided for accepting first and last elements and processing elements in reverse order. Motivating the proposal is a situation in which Java’s collections framework lacks a collection type to represent a sequence of elements with a defined encounter order. It also lacks a uniform set of operations that apply across these collections. These gaps have been a problem and a source of complaints. The proposal calls for defining interfaces for sequencing for collections, sets, and maps, and retrofitting this into the existing collections type hierarchy. All of these new methods have default implementations.
  • String templates, to appear as a preview feature, complement Java’s existing string literals and text blocks by coupling literal text with embedded expressions and processors to produce specialized results. This language feature and API is intended to simplify writing of Java programs by making it easy to express strings that include values computed at runtime. It promises to enhance readability of expressions, improve program security, retain flexibility, and simplify the use of APIs that accept strings written in non-Java languages. Enabling development of non-string expressions derived from combining literal text and embedded expressions also is a goal.

The proposed release schedule for JDK 21 includes rampdown phases occurring June 8 and July 20. The feature set is frozen at the first rampdown phases while bug fixes continue. This is followed by initial and final release candidates on August 10 and August 24, with bug fixes still possible, followed by general availability on September 19.

As a long-term support (LTS) release, JDK 21 would get five years of Premier support and extended support until September 2031. The current LTS release is JDK 17, published in September 2021. Non-LTS releases, such as the JDK 20 release that arrived March 21, and the previous JDK 19 release, receive only six months of Premier support and no extended support.

New versions of Oracle’s standard Java implementation arrive every six months. Other possible features for JDK 21 include all the incubating and preview features in JDK 20, such as scoped values, record patterns, and pattern matching for switch statements. Universal generics, the generational Z garbage collector, and the asynchronous stack trace VM API could also be included.

Copyright © 2023 IDG Communications, Inc.

How to choose a low-code development platform