NashTech Blog

Java 22: What’s New and How It Compares to Java 21

Picture of Agnibhas Chattopadhyay
Agnibhas Chattopadhyay
Table of Contents
Java 22 logo

Introduction

With the release of Java 22 just around the corner, excitement is building within the developer community. This latest version of the beloved programming language brings a host of new features and improvements that promise to enhance both the development experience and application performance. Whether you’re an advanced developer or just starting out, staying updated with the latest Java version is crucial to ensure your projects benefit from the latest advancements and maintain compatibility with future updates.

Key Features of Java 22

Unnamed Patterns and Variables

Java 22 introduces unnamed patterns and variables, allowing developers to use a single underscore (_) to mark variables that are not intended for use. This feature simplifies code and avoids unnecessary naming of variables, particularly useful in cases like exception handling and lambda expressions.

Improvements to G1 Garbage Collector

In this version, the G1 garbage collector has been enhanced with region pinning, which reduces latency by allowing garbage collection even when critical objects are held. This improvement addresses the significant impact on latency observed in previous versions and ensures smoother performance for Java applications interacting with native code.

Foreign Function and Memory (FFM) API Finalization

The FFM API, a major component of Project Panama, has been finalized in Java 22. This API enables Java programs to efficiently interact with native code and manage off-heap memory, offering a more robust and performance-oriented alternative to the Java Native Interface (JNI).

Multi-source File Programs

Java 22 simplifies the execution of multi-source file programs. Developers can now run multiple source files and their JAR dependencies directly using the java command without the need for explicit compilation. This feature streamlines the development process and reduces the complexity of managing dependencies.

Other Notable Features
  • Prologue Statements: Java 22 allows statements before explicit constructor invocations, enabling argument validation and preprocessing.
  • Classfile API: A new API for bytecode parsing, generating, and manipulating, replacing ASM as the standard for bytecode manipulation.
  • String Templates: Enhanced support for embedding expressions in strings, simplifying string concatenation and improving readability.

Detailed Comparison Between Java 22 and Java 21

Unnamed Patterns and Variables

In Java 21, unnamed patterns and variables were introduced as a preview feature. Java 22 finalizes this feature, allowing developers to use underscores in various contexts, such as local variables, pattern variables, and lambda parameters. This change eliminates the need for naming variables that are never referenced, enhancing code clarity and reducing boilerplate.

G1 Garbage Collector

The introduction of region pinning in the G1 garbage collector addresses significant latency issues observed in Java 21. By avoiding the collection of regions containing critical objects, Java 22 ensures smoother performance, particularly for applications that interface with native code.

FFM API

The FFM API has evolved significantly from its preview in Java 21. Java 22 finalizes this API with minor changes, providing a robust framework for interacting with native code and managing off-heap memory. This finalization marks a significant milestone in Project Panama, enhancing Java’s capabilities in high-performance computing scenarios.

Multi-source File Programs

Java 22’s ability to run multi-source file programs directly simplifies the development process. Unlike Java 21, where explicit compilation of each file was necessary, Java 22 allows developers to run complex programs with a single command, making the transition from single-source to multi-source projects seamless.

Performance Analysis

Benchmarks

Based on the benchmarks conducted by Timefold Solver(a tool from Timefold AI), Java 22’s performance remains largely unchanged compared to Java 21. The micro-benchmarks focused on the score calculation part of the solver, showing no statistically significant difference in performance between the two versions. However, the use of GraalVM for JDK 22 demonstrated significant performance improvements, with up to 15% better performance in specific benchmarks.

In real-world scenarios, the performance of OpenJDK 22 remains consistent with Java 21. The benchmarks confirmed the micro-benchmark results, indicating negligible differences between the two versions. However, GraalVM for JDK 22 continued to show performance enhancements, suggesting it as a viable option for applications requiring high throughput.

This Timefold AI blog: https://timefold.ai/blog/java-22-performance provides detailed info on performance and benchmarks and discusses the differences in detail.

Practical Use Cases and Examples

Unnamed Variables in Exception Handling
javaCopy codetry {
    int myInt = Integer.parseInt("12b");
} catch (NumberFormatException _) {
    System.out.println("That is not a number");
}
Lambda Expressions with Unused Variables
javaCopy codeList<Cat> cats = List.of(new Cat(), new Cat(), new Cat());
cats.forEach(_ -> System.out.println("Yay, cats!"));
Running Multi-source File Programs
shellCopy codejava Test.java

Given a project structure where Test.java references other source files.

String Templates
javaCopy codeString name = "John";
int age = 35;
String message = STR."Hello, my name is \{name}! I am \{age} years old.";
System.out.println(message);

Conclusion

Java 22 brings a host of new features and improvements that enhance both the development experience and application performance. While the performance differences between Java 22 and Java 21 are negligible, the new capabilities, such as unnamed variables, improved garbage collection, and multi-source file execution, offer significant benefits. Additionally, the use of GraalVM for JDK 22 can provide notable performance enhancements. For further details on Java 22 refer to Java 22 blog by Oracle.

We encourage developers to try out Java 22 and explore its new features. Staying updated with the latest Java version ensures your projects benefit from the latest advancements and maintain compatibility with future updates.

For more such interesting blogs on latest and upcoming technologies check out NashTech Blogs.

Picture of Agnibhas Chattopadhyay

Agnibhas Chattopadhyay

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading