Press ESC to close

JDK 21: The new features in Java 21

Introduction to JDK 21

Java Development Kit 21, which is the most recent long-term support (LTS) edition of Oracle’s standard Java implementation, has been published. It provides 15 features, such as a key containment mechanism API, virtual threads, a string template, and structured concurrency previews. Oracle plans to maintain support for JDK 21 and Java 11.

The 15 features of JDK 21 

1. JDK 20 and 19 introduced structured concurrency, which simplifies concurrent programming by considering linked processes as a single unit of work. This enhances mistake detection, cancellation, dependability, and observability. The StructuredTaskScope:: method is included in the preview API in Java.util.concurrent package. Instead of a Future, the Fork function returns a Subtask.

2. Scoped values, which were introduced in JDK 20, allow for the transfer of immutable data between and across threads, which is preferable to thread-local variables owing to design issues. This approach prioritises usability, comprehensibility, robustness, and performance above method arguments in big programs.

3. A proposal to disable dynamic agent loading in Java Virtual Machines (JVMs) seeks to enhance integrity by default. The strategy calls for sending alerts when agents are dynamically loaded, reassessing the balance between functionality and integrity, and ensuring that most tools are unaffected. It also integrates dynamic agent loading with additional features such as deep reflection. Agents, which were introduced in JDK 5, can change application code at will, and JDK 21 intends to require app owner agreement for dynamic agent loading, bringing the Java platform closer to authenticity by default.

4. The proposal seeks to develop an API for key encapsulation mechanisms (KEM) to safeguard symmetric keys using public cryptography. It will enable programs to use KEM algorithms in protocols like TLS and HPKE, as well as execute them in Java or native code, for example, the Diffie-Hellman KEM.

5. The proposal calls for the Windows 32-bit x86 port to be dropped in a future version, the build system to be changed so that an error notification is generated, and the port to be labelled.

6. The proposal calls for the Windows 32-bit x86 port to be deprecated in a future version, to upgrade the build system to provide an error message, and to label the port as outdated in the documentation.

7. The suggestion seeks to improve record pattern readability by removing superfluous nested patterns and identifying variables that have to be stated but will not be utilized.

8. When compared to non-generational ZGC, generational ZGC improves application performance by splitting generations for young and old objects, lowering allocation stall hazards, heap memory overhead, and garbage collection CPU overhead, while retaining considerable throughput.

9. Record patterns, suggested for data navigation and processing in JDK 19 and 20, attempt to break down record values and enable layered patterns. The latest JDK Enhancement Proposal will wrap up the functionality with feedback-based enhancements. The key change is the removal of record pattern support at the beginning of improved statements.

10. Switch pattern matching enables sophisticated data-oriented queries to be phrased securely and simply. The proposal is expected to be completed by JDK 21. Changes include the removal of parenthesized patterns and the addition of qualified enum constants. Extending expressiveness, relieving historical null-hostility, and boosting switch statement safety are all goals.

11. The sixth vector API incubation attempts to deliver reliable vector calculations on CPU architectures, outperforming scalar computations. The proposal contains speed improvements and bug fixes, as well as platform independence and gradual degradation.

12. Java 21 includes a preview of a foreign function and memory API, which will allow Java programs to communicate with code and data outside of the Java runtime. The API effectively executes foreign methods and safely exposes foreign memory, allowing Java programs to call native libraries and handle native data without the risk of JNI.

13. Virtual threads are lightweight threads that make it easier to write, manage, and monitor high-throughput concurrent applications. They enable servers to scale with near-optimal hardware utilization and to modify existing code using the lang. Thread API, and debug and profile easily using current JDK tools.

14. The proposal for sequenced collections intends to fill holes in Java’s collections framework by offering interfaces for collections with a specified encounter order and standard actions. It suggests creating interfaces for sequencing collections, sets, and maps, retrofitting them to fit the existing collection type hierarchy, and providing default implementations.

15. String Templates are a preview feature introduced in JDK 21 that mixes Java’s string literals with embedded phrases to provide specialized outputs. This feature seeks to simplify Java programme authoring, improve readability, increase security, maintain flexibility, and allow non-string expression development.