Martin Ahrer

Thinking outside the box

Java 22 major language and API improvements

2024-09-10 3 min read Martin

Java 22 has been released in March 2024.

The following content is summarizing the most important changes to the Java API, the Java language, and the JVM introduced with Java 22.

For a more complete overview follow the links in the following sections referring to the official Oracle release documents.

Java 22 Language improvements

The following describes selected improvements of the Java language. See the Java 22 release notes. See Java Language Updates for Java SE 22.

With Java 22 the following language enhancements are now generally available

  • JEP 456 is releasing Unnamed Patterns and Variables. Unnamed patterns, which match a record component without stating the component’s name or type, and unnamed variables, which can be initialized but not used. Both are denoted by an underscore character, _.

Java 22 is introducing the following language enhancements as developer preview.

  • JEP 447 is starting with Statements before super(…​) as developer preview. In constructors in the Java programming language, allow statements that do not reference the instance being created to appear before an explicit constructor invocation.

  • JEP 459 is continuing with String Templates as developer preview. String templates complement Java’s existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.

  • JEP 463 is starting with Implicitly Declared Classes and Instance Main Methods.

We are not diving into the details of these preview features and defer that until they are moving from preview for general availability.

Unnamed Variables And Patterns

The Java language mandates to declare variables (names) even when they are not needed (used). Often we also find ourselves to declare variables just for the sake of having a consistent look.

Unnamed Variables
var string = "abc";
try {
  int _ = Integer.parseInt(string);
} catch (NumberFormatException _) {
  System.out.println("Not a number: " + string);
}
Unnamed Variables For Consistent Code Style
var values = new Stack<String>();
values.push("1");
values.push("2");
values.push("3");

String _ = values.pop(); (1)
String _ = values.pop();
String value3 = values.pop();

try {
  int number = Integer.parseInt(value3);
} catch (NumberFormatException _) { (2)
  System.out.println("Not a number: " + value3);
}
1The value is not used, but we want to maintain a consistent code style.
2The exception object is not used, but we have to declare a variable here to satisfy the compiler.
Unnamed Variables And Patterns
 interface Part {
 }
 record Circle(int radius) implements Part {
 }
 record Square(int length) implements Part {
 }

 Part part = new Square(4);

switch (part) {
   case Square _ -> System.out.println("Square");
   case Circle _ -> System.out.println("Circle");
   case null, default -> throw new IllegalStateException("Unexpected value: " + part);
 }

Unused variables and patterns also work with instanceof. See JEP-456.

Java 22 API improvements

The following describes selected improvements of the Java API. See the Java 22 release notes.

Java 22 is introducing the following API enhancements as developer preview or Incubator.

  • JEP 446 is continuing with scoped values, which enable the sharing of immutable data within and across threads. They are preferred to thread-local variables, especially when using large numbers of virtual threads

  • JEP 457 is starting with a Class-File API to provide a standard API for parsing, generating, and transforming Java class files. This could eliminate the need for libraries like ASM.

  • JEP 453 is continuing with simplifying multithreaded programming by introducing an API for structured concurrency. Structured concurrency treats multiple tasks running in different threads as a single unit of work, thereby streamlining error handling and cancellation, improving reliability, and enhancing observability..

  • JEP 461 is starting Stream Gatherers. This will allow stream pipelines to transform data in ways that are not easily achievable with the existing built-in intermediate operations

We are not diving into the details of these preview features and defer that until they are moving from preview for general availability.