Java JDK 21: The key to the Door

Java JDK turns 21 (well it's the 21st release) on the 19th of September 2023. Will this turn out to be as significant an event as a 21st birthday?

18-09-2023
Bcorp Logo


Java JDK 21: The key to the Door


Turning 21

Many moons ago, turning 21 signified entry into adult life and the independence that this brought – in many cases a symbolic ‘key to the door’ was presented to birthday boy or girl. Of course, nowadays the entry to adult life is often earlier and a key to the door is obtained at a much younger age, nevertheless turning 21 is still often considered a significant moment in one’s life. Java JDK turns 21 (well it's the 21st release) on the 19th of September 2023. Will this turn out to be as significant an event as a 21st birthday?

LTS and Non -LTS

Java SE 21 aka JDK 21 is certainly a significant event from one perspective; it is the next Long term Support or LTS version. The last LTS version was Java SE 17 back in September 2021, prior to this there were two previous LTS versions Java SE 11 in September 2018 and Java SE 8 back in March 2014. Each of these is a significant release as it is only the LTS versions which are supported for more than six months.

Intermediate releases such as Java SE 19 in September 2022 and Java SE 20 in March 2023 are only supported until the next release of Java which is usually six months. These versions should not therefore be used for production systems relying on a stable and supported Java environment; instead, they act as preview versions introducing new or revised features in the language of the Java runtime etc. It is only the LTS versions that should be considered suitable target versions of a commercial or professional systems.

This new LTS version of Java introduces numerous JEPs that have been previewed in recent releases of Java, but which are now ready for production use.

A JEP is a Java Enhancement Proposal and is the main way in which new features can be introduced into the language or existing features revised or modified. Each JEP will target a particular aspect, although some of these aspects can be very specific such as a change to the way in which strings can be defined, others can be very quiet ranging such as introducing new garbage collection approaches. In addition, some JEPs are introduced in incubator or preview modules. An incubator module is published to obtain developer feedback – such a module could be altered or indeed disappear completely before being made part of the core language. Preview modules are fully specified and implemented features but might change in the future and therefore should not be used in production code.

To JEP or not to JEP

In Java SE 21 there are 15 JEPs, four of these are final (or release) six are previews (and therefore subject to change) and one is an incubator module. The final four JEPs don't change the language or any of the libraries but may have a significant impact on the performance of the system or may modify or introduce new environmental or operational features.

The final, fully stable JEPs are:

These are the preview JEPs.

The one incubator JEP is:

The final four JEPs are:

Each of the final JEPs will be summarized in the remainder of this blog along with a summary of the preview, incubator and additional JEPs.

Java JDK 21: The key to the Door


JEP 431: Sequenced collections

The Collections API has been around since Java JDK 1.1 and although there have been a few changes over the years, little has changed in the world of collections. This JEP introduces a set of interfaces that represent collections with a well-defined specific order. This JEP provides a set of uniform APIs for first, last, reverse etc. style behaviours. These interfaces will be added to collection implementation classes as appropriate.

JEP 440: Record patterns

This JEP enhances the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable declarative, and composable form of data navigation and processing. For example, it is now possible to write code such as:

void printPoint(Object obj) {
    if (obj instanceof Point(int x, int y)) {
        System.out.println(x, y);
    }
}

This JEP can be combined with JEP 441 so that the feature can be used in switch statements.

JEP 441: Pattern matching for switch

This JEP was initially proposed in Java 17 and subsequently refined in Java 18 through Java 20. It will be finalized in Java 21. This new switch statement is more concise and easier to use and supports enumerated types as well as records. For example, it is now possible to write the following:

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();
    };
}

It is also possible to provide guards on a case condition such as:

void example(Object obj) {
    switch (obj) {
        case R r when (r.i / 0 == 1): System.out.println("It's an R!");
        default: break;
    }
}


Java JDK 21: The key to the Door


JEP 444: Virtual threads

This JEP is potentially why developers might decide to move Java SE 21 sooner rather than later. Virtual threads were previously introduced in preview JEPs in both Java 19 and Java 20, and they will reach their finalized state in Java 21.

Oracle themselves say that this is one of the most significant updates in Java’s history. It represents lightweight threads that offer the potential to significantly simplify the process of developing, maintaining, and monitoring concurrent applications that demand high throughput.

The primary objective of this JEP enables the implementation of thread-per-request style server side application that will make optimal use of the server hardware.

The Preview JEPs

The preview JEPs include several interesting JEPs, for example the String templates JEP (JEP 430) allow string literals to contain embedded expression simpler for each to f strings in Python. The foreign function and memory API explores interacting with code and data beyond the Java world while the unnamed classes and instance main methods preview (JEP 445) will simplify the creation of applications such that a main method can be written without the need to define an encompassing class – which will make it simpler to get a basic application up and running. For example, instead of writing:

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

All that will be required is:

void main() {
  System.out.println("Hello world!");
}

This is still a preview feature but is very likely to be introduced in a future version of Java.

The Incubator JEP

The single incubator JEP is now in its sixth incubation release – it is intended to allow vector computations that are compiled and executed as efficiently as possible on supported CPU architectures.

The remaining JEPs

The Generational ZGC JEP aims to enhance application performance. The Windows 32-bit x86 port is slated for deprecation and eventual removal. JEP 449 aims to enhance the build system, causing it to generate an error message when it configures a build for Microsoft Windows 32-bit x86. JEP 452 (Prepare to disallow the dynamic loading of agents) issues warnings when agents are dynamically loaded into the JVM. Finally, JEP 452 (Key encapsulation mechanism API) enables the functioning of key encapsulation mechanism (KEM) algorithms which are cryptographic methods used to safeguard symmetric keys through public cryptography.

Summary

Should you be excited about Java SE 21? Probably, the most significant aspects of the release are the introduction of virtual threads, improvements to Records and the switch statement and finally the Generational Z Garbage Collector. It is of course also a LTS version which means that it is suitable for production systems and can be used as such from September 2023. How significant these changes turn out to be in the long run only time will tell but the future is looking good, and it starts with Java SE 21.


Would you like to know more?

Take a look at our Java training courses or more specifically Java Training Course (Introduction to Java 21+)

Share this post on:

We would love to hear from you

Get in touch

or call us on 020 3137 3920

Get in touch