DEV Community

Hanniel Vieira
Hanniel Vieira

Posted on

News about JDK 21 update

Hello, developer, how are you? Today, on September 19th, the JDK 21 version was released. But what can we expect from version 21?

JDK 21 is the new LTS (Long-Term Support) version, which means it will receive support for a longer period than other versions, a common concept in many software products, including operating systems and programming languages. It is designed to meet the needs of organizations that want stability and continuity in their software implementations and is typically maintained for 5 years.

What are the main changes?

With the arrival of JDK 21, several features have been implemented. Let's talk a bit about them.

Language Feature

JEP 440: Records Patterns

JEP 440 aims to enhance the Java programming language with record patterns for deconstructing record values. Record patterns and type patterns can be nested to allow powerful, declarative, and composable ways of navigating and processing data.

But what exactly is a record pattern?

A record pattern consists of a record class type and a list of patterns (possibly empty) that are used to match the values of corresponding record components. For example, given the declaration:

Record Person(String name, String lastName) {}
The Person value will be recorded as an initialized value without the need for creating getters, setters, and constructors, making it very useful for DTO patterns.

JEP 441: Pattern Matching for switch

JEP 441 aims to enhance pattern matching for switch expressions and statements. It allows an expression to be tested against multiple patterns, each with a different action, enabling concise and safe expression of complex data-driven queries. For example, given the declaration:

static String formatterPatternSwitch(Object obj) {
    return switch (obj) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> obj.toString();
    };
}
Enter fullscreen mode Exit fullscreen mode

In summary, JEP 441 updates and simplifies various ways of using the switch statement, making Java less verbose and cleaner, which is helpful for newcomers to the language and streamlining repetitive developer processes.

Language Features Previews

JEP 430: String Templates (Preview)

JEP 430 enhances the Java programming language with string templates. String templates complement existing string literals and text blocks in Java by combining literal text with embedded expressions and template processors to produce specialized results.

But why template strings? String templating aims to make string interpolation safer. String interpolation is a technique that allows you to embed variable values or expressions directly into a string, making the process of creating strings more convenient and readable. Instead of manually concatenating strings and variables, you can insert placeholders in the string and fill them with desired values.

Unfortunately, the convenience of interpolation has a drawback: it's easy to build strings that will be interpreted incorrectly by other systems. This is what JEP 430 seeks to address.

JEP 443 and JEP 445: Unnamed Patterns and Variables. Unnamed Classes and Instance Main Methods (Preview)

Both JEP 443 and JEP 445 aim to reduce the verbosity of Java to simplify it for new Java programmers.

Libraries Improvements

JEP 444: Virtual Threads

Introduce virtual threads to the Java Platform. Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.
Main objectives:

  • Enable server applications written in the simple thread-per-request style to scale with near-optimal hardware utilization.

  • Enable existing code that uses the java.lang.Thread API to adopt virtual threads with minimal change.

  • Enable easy troubleshooting, debugging, and profiling of virtual threads with existing JDK tools.

JEP 431: Sequenced Collections

Introducing new interfaces to represent collections with a defined encounter order. Each of these collections has a well-defined first element, second element, and so on, up to the last element. It also provides uniform APIs for accessing their first and last elements and for processing their elements in reverse order.

JEP 452: Key Encapsulation Mechanism API

Introducing an API for Key Encapsulation Mechanisms (KEMs), a cryptographic technique for protecting symmetric keys using public-key cryptography.

Library Improvements Previews and Incubator

JEP 442: Foreign Function & Memory API (Third Preview)

Introducing an API through which Java programs can interoperate with code and data outside the Java runtime. By efficiently invoking external functions (i.e., code outside the JVM) and safely accessing external memory (i.e., memory not managed by the JVM), the API allows Java programs to call native libraries and process native data without the fragility and danger of JNI.

JEP 453: Structured Concurrency (Preview)

Simplifying concurrent programming by introducing an API for structured concurrency. Structured concurrency treats groups of related tasks executed on different threads as a single unit of work, simplifying error handling, improving reliability, and enhancing observability.

JEP 446: Scoped Values (Preview)

Introducing scoped values, which can be safely and efficiently shared across methods without using method parameters. They are preferred over thread-local variables, especially when using a large number of virtual threads.

JEP 448: Vector API (Sixth Incubator)

Introducing an API to express vector computations that are reliably compiled at runtime to ideal vector instructions on supported CPU architectures, achieving superior performance compared to equivalent scalar calculations.

Performance Improvements

JEP 439: Generational ZGC

Improving application performance by extending the Z Garbage Collector (ZGC) to maintain separate generations for new and old objects. This allows the ZGC to collect young objects which tend to die young more frequently.
Applications running with Generational ZGC should benefit from

  • reduced allocation pause times,
  • lower heap memory overhead,
  • lower CPU overhead for garbage collection.

Stewardship

JEP 451: Prepare to Disallow the Dynamic Loading of Agents

Issuing warnings when agents are dynamically loaded into a running JVM. These warnings are intended to prepare users for a future version that will disallow dynamic loading of agents by default to improve default integrity. Maintenance tools that load agents at startup will not trigger warnings in any version.

This was a brief overview of JDK 21, the LTS version of Java. If you have any questions, I'm available to explain what I understand and to check for any new updates and documentation regarding Java JDK 21 Click here.

Top comments (1)

Collapse
 
stherzada profile image
Sther

Nice!