What Is New in Java 22
Java 22 delivers a suite of updates including language enhancements, performance boosts, and new APIs. This release continues to focus on developer productivity and application modernization.
| Category | Key Changes |
|---|---|
| New Features | Unnamed Variables & Patterns, String Templates (Preview), Implicitly Declared Classes (Preview), Foreign Function & Memory API |
| Improvements | Launch Multi-File Source-Code Programs, Stream Gatherers (Preview), Region Pinning for G1 GC, Class-File API (Preview) |
| Bug Fixes | Various fixes across core libraries, hotspot, and security components |
| Deprecations | Deprecation of the Solaris and SPARC Ports |
What are the key language improvements in Java 22?
Java 22 introduces preview features that streamline common coding patterns. Unnamed variables and patterns allow you to use underscores for unused bindings, reducing clutter.
String Templates offer a safer alternative to string concatenation, integrating expression evaluation directly. This matters because it helps prevent SQL injection and other security issues common in string building.
String name = "Joan";
String info = STR."My name is \{name}";
Implicitly declared classes let beginners write simple programs without the full class declaration syntax, lowering the barrier to entry.
How does Java 22 enhance performance and garbage collection?
The G1 garbage collector gets region pinning support, which reduces latency during JNI critical sections. This is a big deal for applications that make frequent native calls, as it minimizes GC-induced pauses.
Streams become more powerful with the Stream Gatherers preview API. It enables custom intermediate operations, going beyond the built-in map, filter, and reduce.
List<String> list = Stream.of("a", "b", "c")
.gather(Gatherers.windowFixed(2))
.toList(); // Results in [["a", "b"], ["c"]]
What new APIs should developers explore?
The Foreign Function & Memory API finally graduates from preview to a standard feature. You can now reliably call native code and manage off-heap memory without the brittleness of JNI.
Another significant addition is the Class-File API, which provides a standard way to parse, generate, and transform Java class files. Library and framework developers will find this invaluable for bytecode manipulation.
You can now run multi-file source-code programs directly with the java launcher. This simplifies running small applications spread across a few files without an explicit compilation step.
Are there any notable deprecations or removals?
Yes, the Solaris and SPARC ports have been officially deprecated for removal. If your infrastructure still relies on these platforms, start planning your migration now.
In practice, this won't affect the vast majority of deployments, which run on Linux, Windows, and macOS on x64 and AArch64 architectures. It's a cleanup of legacy hardware support.
FAQ
Is the Foreign Function & Memory API production-ready now?
Yes, it has graduated from its preview status and is now a standard, supported API in Java 22. You can use it for native interop without fearing API changes in future releases.
What's the point of unnamed variables?
They make code more readable by explicitly ignoring a value. Instead of naming a variable you won't use, like in a catch block or lambda, you can just use an underscore.
Can I use String Templates for SQL queries safely?
Absolutely. The API is designed to be validated by a template processor, which can sanitize inputs to prevent injection attacks, making it far safer than manual string concatenation.
What replaces the deprecated Solaris/SPARC support?
There is no direct replacement within the JDK. The recommendation is to migrate to a supported operating system and architecture, such as Linux on x64 or AArch64.
Do I need to compile .java files before running with the new launcher?
No. For multi-file source programs, you can execute them directly using `java --source 22 MyApp.java`, and the launcher will handle the compilation in memory.