top of page
Search

Java 22 Polishes the Future of Java

  • schick09
  • Jul 28
  • 3 min read

Java 22, released in March 2024, is another feature release (non-LTS), and it continues to refine many of the capabilities introduced in Java 21.


With Java 21 having locked in virtual threads, pattern matching, and record patterns, Java 22 shifts focus to enhancing preview features, streamlining syntax, and finalizing some key APIs.


This release is especially useful for developers building scalable systems, experimenting with native code, or working on cleaner syntax in modern Java.


Let’s take a look at what Java 22 brings to the table.



✅ Final Features in Java 22


1. Foreign Function & Memory API — JEP 454


The Foreign Function & Memory (FFM) API is now finalized, replacing JNI for many use cases. It provides a powerful and safe way to:


  • Call native functions (e.g., from C)

  • Allocate and manage off-heap memory

  • Share memory with native processes


For example:


try (Arena arena = Arena.ofConfined()) {

MemorySegment segment = arena.allocate(100);

// Work with native memory

}


This final version includes:

  • Stable memory layout APIs

  • Linker for native function calls

  • SymbolLookup for native symbol resolution




2. Statements Before super() — JEP 447


This long-requested enhancement allows statements before super() in constructors—as long as they don’t reference this.


Example:


class Child extends Parent {

Child(String raw) {

String cleaned = raw.trim();

super(cleaned); // Now allowed

}

}


Why it matters:

  • Useful for validating or preparing constructor arguments

  • Improves readability and modularity





🧪 Preview and Incubator Features


3. Unnamed Variables and Patterns (Preview) — JEP 443


You can now use _ as a placeholder for:

  • Unused variables

  • Unused pattern bindings


For example:


if (obj instanceof String _) {

System.out.println("It’s a String, but we don’t care what it is.");

}


This reduces boilerplate and improves clarity in pattern matching.




4. Unnamed Classes and Instance main Methods (2nd Preview) — JEP 456


First previewed in Java 21, this feature simplifies the process of writing small programs.


For example:


void main() {

System.out.println("No class declaration required!");

}


This version enhances integration with tooling and improves handling of default behaviors.





5. String Templates (2nd Preview) — JEP 459


String Templates continue to evolve with better IDE support, performance, and custom processors.


For example:


String name = "Alice";

String greeting = STR."Hello, \{name}!";


You can also define your own processors—for example, to safely build SQL or JSON.





6. Structured Concurrency (2nd Preview) — JEP 462


This update brings minor refinements to structured concurrency, especially around task scope management and error propagation.





7. Stream Gatherers (Preview) — JEP 461


A powerful new addition to the Stream API, gatherers allow for custom reduction operations, beyond what Collectors offer.


For example:


Stream<String> stream = ...

var custom = stream.gather(Gatherers.windowFixed(3));



This opens up new ways to work with sliding windows, batching, grouping, and more.





✅ Summary Table

Feature

JEP

Status

Type

Foreign Function & Memory API

454

Final

API

Statements Before super()

447

Final

Language

String Templates

459

2nd Preview

Language/API

Structured Concurrency

462

2nd Preview

API

Unnamed Variables and Patterns

443

Preview

Language

Unnamed Classes and main()

456

2nd Preview

Language

Stream Gatherers

461

Preview

API



Should You Upgrade to Java 22?


Java 22 is a solid step forward, especially for developers interested in:

  • Native interop using the finalized FFM API

  • Cleaner constructor and pattern syntax

  • Continued experimentation with Loom and Amber features


⚠️ Note: As a non-LTS release, Java 22 is not ideal for production environments that require long-term support. Java 21 (LTS) is the better choice unless you're actively experimenting or developing cutting-edge libraries.


Final Thoughts


Java 22 shows that the platform is not slowing down. The Java team continues to focus on expressiveness, performance, and modern developer experience.

Whether you’re working with native code, scaling apps with virtual threads, or crafting clean DSLs with string templates, Java 22 has something to offer.


ree

 
 
 

Comments


Dave Schick Consulting

©2023 by Dave Schick Consulting. Proudly created with Wix.com

bottom of page