Introduction

Java 21 is released on September 19, 2023. Java 21 is the next LTS version after Java 17. It’s expected that users of Java 17 will gradually migrate to Java 21. This book summaries all major changes from Java 17 to Java 21, so you can easily migrate from Java 17 to Java 21.

The most important change in Java 21 is the introduction of virtual threads. Virtual threads will dramatically change multi-threading programming in Java.

If you are currently using Java 11, check out my book From Java 11 to Java 17 for upgrading from Java 11 to Java 17 first.

JEPs in Different Java Releases

Java Enhancement Proposal (JEP) is used to organize changes to OpenJDK. A Java release may contain a list of JEPs. Below are lists of JEPs added in each Java release from Java 18 to Java 21.

Java 18

Java 18 has 9 JEPs, see the table below.

Figure 1. JEPs in Java 18
Number Name
400 UTF-8 by Default
408 Simple Web Server
413 Code Snippets in Java API Documentation
416 Reimplement Core Reflection with Method Handles
417 Vector API (Third Incubator)
418 Internet-Address Resolution SPI
419 Foreign Function & Memory API (Second Incubator)
420 Pattern Matching for switch (Second Preview)
421 Deprecate Finalization for Removal

Java 19

Java 19 has 7 JEPs, see the table below.

Figure 2. JEPs in Java 19
Number Name
405 Record Patterns (Preview)
422 Linux/RISC-V Port
424 Foreign Function & Memory API (Preview)
425 Virtual Threads (Preview)
426 Vector API (Fourth Incubator)
427 Pattern Matching for switch (Third Preview)
428 Structured Concurrency (Incubator)

Java 20

Java 20 has 7 JEPs, see the table below.

Figure 3. JEPs in Java 20
Number Name
429 Scoped Values (Incubator)
432 Record Patterns (Second Preview)
433 Pattern Matching for switch (Fourth Preview)
434 Foreign Function & Memory API (Second Preview)
436 Virtual Threads (Second Preview)
437 Structured Concurrency (Second Incubator)
438 Vector API (Fifth Incubator)

Java 21

Java 21 has 15 JEPs, see the table below.

Figure 4. JEPs in Java 21
Number Name
430 String Templates (Preview)
431 Sequenced Collections
439 Generational ZGC
440 Record Patterns
441 Pattern Matching for switch
442 Foreign Function & Memory API (Third Preview)
443 Unnamed Patterns and Variables (Preview)
444 Virtual Threads
445 Unnamed Classes and Instance Main Methods (Preview)
446 Scoped Values (Preview)
448 Vector API (Sixth Incubator)
449 Deprecate the Windows 32-bit x86 Port for Removal
451 Prepare to Disallow the Dynamic Loading of Agents
452 Key Encapsulation Mechanism API
453 Structured Concurrency (Preview)

Install Java 21

You can get Java 21 binaries from several different places. Since Java 21 is a LTS version, many vendors will provide Java 21 releases. For most users, Eclipse Temurin is the best choice.

  • Download OpenJDK 21 build from jdk.java.net.
  • Download Eclipse Temurin from Adoptium.
  • Use SDKMAN to install, e.g. sdk install java 21.0.2-open or sdk install java 21.0.8-tem.
  • Use IDE to download. IntelliJ IDEA can download JDK.

You can also get OpenJDK 21 releases from other vendors, like Amazon Corretto, Oracle, and Zulu. For example, you can use sdk install java 21.0.8-amzn to install Amazon Corretto 21.

Below is the output of java -version for OpenJDK JDK 21 build.

Figure 5. Output of java -version
1 openjdk version "21" 2023-09-19
2 OpenJDK Runtime Environment (build 21+35-2513)
3 OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)

Build Tools

When using Java 21 with build tools, Maven or Gradle, you may need to add the --enable-preview argument to enable preview features. The following code shows an example of the configuration of Maven compiler plugin. jdk.incubator.vector is an incubating module, so it needs to be added explicitly using --add-modules.

Figure 6. Maven compiler plugin configuration
 1 <plugin>
 2     <groupId>org.apache.maven.plugins</groupId>
 3     <artifactId>maven-compiler-plugin</artifactId>
 4     <version>3.13.0</version>
 5     <configuration>
 6         <source>21</source>
 7         <target>21</target>
 8         <compilerArgs>
 9             <arg>--enable-preview</arg>
10             <arg>--add-modules</arg>
11             <arg>jdk.incubator.vector</arg>
12         </compilerArgs>
13     </configuration>
14 </plugin>

Source Code

Source code of this book can be found on GitHub.