Java 21 is here, and it includes many improvements, such as the preview of virtual threads and the final version of the generational ZGC.

Most updates in Java are categorized into different named projects. In this release the features come from three projects: Project Loom, which has to do with concurrency in Java; Project Panama, which is improving how you can connect Java and native code; and Project Amber, which Oracle describes as a project to “explore and incubate smaller, productivity-oriented Java language features that have been accepted as candidate JEPs.”

One of the Project Loom additions in this release is virtual threads, which Georges Saab, senior vice president of development for the Java Platform at Oracle and OpenJDK Chair, describes as being one of the most influential preview features in this release. Virtual threads are “lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications,” as defined in the JEP listing for this feature.

“We’re really excited with how this has come out because it just takes Java scalability to an entirely new level, while not breaking your existing programs,” Saab told SD Times. “And this is something that not only people in the Java space are excited about; we’ve seen people across the industry, even people involved in other language platforms looking at this and saying, ‘Wow, Java really did it right here.’”

Project Loom also includes two preview features: scoped values and structured concurrency. Scoped values are “values that may be safely and efficiently shared to methods without using method parameters,” according to Oracle. Structured concurrency treats groups of related tasks from multiple threads as a single unit, which helps streamline error handling and improve reliability. 

Another major update in this release not tied to a particular named project is the final version of generational ZGC, otherwise known as the Z Garbage Collector. Generational ZGC separates young and old objects so that young objects can be collected more frequently. Some of the benefits of this include lower risk of allocation stalls, lower required heap memory overhead, and lower garbage collection CPU overload. 

Several updates in Java 21 come from Project Amber, such as pattern matching for switch expressions, which allows an expression to be tested against a number of different patterns. This helps data-oriented queries to be expressed more concisely and safely. 

Also from Project Amber are unnamed patterns and variables, and unnamed classes and instance main methods, both of which are in preview. These are directed at making Java more approachable, Saab explained.

“Someone did a study at one point and looked at the ‘Hello World!’ program in Java and noted that you really kind of have to learn about 32 concepts of object-oriented programming in order to get started,” Saab explained. “And we thought that was too many. We thought that in this day and age, we want something that is easier for students to get their heads around, while at the same time, making sure that there’s a very gradual and graceful on-ramp into writing more sophisticated Java programs, and learning the rest of those concepts.”

Other Project Amber features include Record Patterns, which deconstruct record values, and a preview for String Templates, which combines literal text to embedded expressions and template processors.

“As a developer myself, I get really excited about Project Amber and all of the progress it’s made over the last couple of years, including this release, things like record patterns, and records themselves, and pattern matching. All of these things are designed to work very, very nicely together,” said Chad Arimura, vice president of Java Developer Relations at Oracle. 

Coming out of Project Panama are updates to two APIs that are being worked on: the Foreign Function & Memory API (currently in its third preview) and the Vector API (currently in its sixth incubator). 

The Foreign Function & Memory API allows “Java programs to interoperate with code and data outside of the Java runtime.” The Vector API expresses “vector computations that reliably compile at runtime to optimal vector instructions on supported CPU architectures.”

Other new features in this release include Sequenced Collections, Key Encapsulation Mechanism API, deprecation of the 32-bit x86 port, and preparation for disallowing the dynamic loading of agents

“We’re quite pleased with where Java is right now, and couldn’t be more pleased with the innovation pipeline that we have,” said Saab.