Java 22

Java 22 - a brief introduction to the new features

Tomorrow on 19.03.2024 - exactly half a year to the day after the LTS version Java 21 - the new Java version with the number 22 will be released in accordance with the six-monthly release cycle. You can read about the new features of the release in this blog post.

JEP-423: Region Pinning for G1

Java's standard garbage collector G1 can lead to problems in connection with JNI (Java Native Interface), such as blocking an application for several minutes, causing unnecessary out-of-memory states, or premature termination of the JVM. This JEP is intended to solve these problems. If you are interested in the background and technical details, you can download the corresponding JDK Enhancement Proposal No. 423 read through.

Java 22

JEP-454: Foreign Function & Memory API

This new API is intended to replace JNI. It is used by Java programs to interact with code and memory areas outside the Java runtime environment. By efficiently calling external functions (i.e. code outside the JVM) and securely accessing external memory that is not managed by the JVM, the API enables Java programs to call native libraries and process native data without the fragility and danger of JNI (Java Native Interface).

This API has been available as an incubator or preview feature since Java 17. The final, finalized version of the API is now being delivered with Java 22.

Code example 1 - Allocating a foreign memory area:

Arena controls the life cycle of external storage segments and enables both flexible allocation and prompt release.

MemorySegment enables access to a contiguous area of the memory.

The code shown above allocates an off-heap memory area large enough to store 10 values of the primitive type int and fills it with values from 0 to 9. The value at index 3 is then read out and output.

Code example 2 - Calling the external C function "strlen" to determine the length of a string:

The linker class provides access to external functions from the Java code, as well as access to the Java code from the external functions.

MethodHandles have been around since Java 7; they were introduced as a more performant option to Reflection. Now they can no longer only refer to Java methods, but also to external functions.

In the code example above, a memory segment containing the word "Hello" as a C-string is allocated.

The native function call is made via the MethodHandle "strlen" using "invokeExact".

The code examples are taken from the Javadoc of the package "java.lang.foreign" which contains a detailed description of the new API.

JEP-456: Unnamed Variables & Patterns

Introduced as a preview feature in Java 21, this feature is now included unchanged and finalized in Java 22.

Unused components in record patterns or unused variables no longer need to be named. Instead, they can be given the underscore "_" as an identifier. This makes the code easier to understand, as the developer can explicitly express that a certain construct, which must be specified in the code, is not required.

If you only want to use the x-coordinate for Point in a record pattern, but not the y-coordinate, you can write this as follows:

It is also possible to omit the data type:

Unused variables can also be named accordingly outside of patterns:

_ can also be used for exceptions in catch statements:

The same applies to unused parameters in lambdas. In line 6, for example, a CommandLineRunner is defined that does not require the command line parameters:

JEP-458: Launch Multi-File Source-Code Programs

Since JEP-330 In Java 11, it is possible to execute *.java files directly, provided the complete program is contained in the file. An explicit call to the compiler javac is not necessary.

As of Java 22, it is now possible to execute *.java files even if the program they contain consists of several source files.

Given a program that consists of the following two classes:

The program can now be started with the command

It is also possible to include pre-compiled *.class files and *.jar files.

The following variant of the Greeter class uses the StringUtils class of the commons-lang3 library, for example:

If you now store the commons-lang3.jar in a subdirectory called "libs" relative to MultiFileProgram.java, the program can be started as follows:

This extension is intended to make it easier for newcomers to get started with Java and to keep the initial effort for the project setup as low as possible. For simple multi-source programs, there is no need to deal with the complexity of a build tool.

And the preview features?

Of course, Java 22 also contains a whole range of exciting preview features. I will be covering these in a separate blog post shortly.

 

Learn more about Java programming

Stefan Waldmann

About ME

Stefan Waldmann has a degree in business informatics and works at doubleSlash as Senior Software Engineer and Lead Developer. In his many years of IT project experience, he has worked, for example, with the Deutsche Telekom AG and the German Post worked together. Among other things, he is an expert in system integration and interfaces as well as Unit testing, test-driven development and software quality. Stefan Waldmann is familiar with many different enterprise software architectures and has extensive experience in the design, integration, development and project management of Java EE projects.

All contributions from Stefan Waldmann

Learn more

Further information on our website and in our newsletter

Arrow up