JDK 21: The new features in Java 21

Java Development Kit (JDK) 21, due in September as the next long-term support release of Oracle’s standard Java implementation, is beginning to take form, with sequenced collections, string templates, and virtual threads slated for the release so far.

Virtual threads is a significant improvement to Java concurrency that was added to the JDK 21 page in late last week. Although the JDK 21 release page still lists only two official features, sequenced collections and virtual threads, string templates also is slotted for JDK 21, according to its Java Enhancement Proposal at

The specific proposals include:

  • 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.

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button