I work with Java since 2011 and these last years we are seeing awesome new features in the language that are long time requests from the Java community, some of these already exists in other languages, so it’s awesome to see this evolution.

Let’s explore the key features introduced in Java versions 22 and 21 compared to previous versions.

This Monday, April 1st 05 PM US EST / 18:00 BRT / 21:00 UTC, I’ll be joining an awesome team of developers in “The Out of the Box Developer” live to talk about this Java 22 evolution, looking back to the latest enhancements from Java 22, 21 and even going further, looking to the Java evolution in these latest versions.

You’re more than welcome to join our conversation live here or watch it if you’re seeing this post after the live.

Subscribe to our newsletter!

[newsletter_form type=”minimal”]

Here is a briefing of the content of this live (we can cover all of these and more or just few topics from it, here are some examples of the new features showed in Java 22 and Java 21):

Java 22 new features

The goal of Oracle with this version is to make Java more friendly for new developers looking to learn Java, but you will likely need to know how to use the previous versions as well to be able to develop in any already existing application.

1. Unnamed Variables & Patterns (JEP 456):

We have started to see some of these in Java 21 but now it’s mature and complete. This feature allows you to use an underscore (_) to denote variables or patterns that are intentionally unused, improving code readability. Here are some examples:

// Example#1: Using in an Enhanced For Loop
public record Employee (String name) {}
for (Employee _ : employees) {
    total++;
    if (total > limit) {
        //logic
    }
}

// Example#2: Using in a Try-Catch Block
try {
    int number = Integer.parseInt (string);
} catch (NumberFormatException _) {
    System.err.println ("Not a number");
}

2. Statements Before super

Java 22 introduces the ability to have statements before the explicit invocation of constructors. This simplifies logic in static helper methods and intermediate constructors.

Example:

public class PositiveBigInteger extends BigInteger {
    public PositiveBigInteger(long value) {
        if (value <= 0)
            throw new IllegalArgumentException("non-positive value");
        super(Long.toString(value));
    }
}

3. Structured Concurrency

With Structured Concurrency, Java now supports virtual threads, making concurrency more manageable and scalable.

Example:

try (var scope = new StructuredTaskScope<Object>()) {
    Subtask<String> subtask1 = scope.fork(task1);
    Subtask<Integer> subtask2 = scope.fork(task2);
    scope.join();
    // Process results/exceptions...
}

4. Foreign Function & Memory API

Project Panama improves interoperability with native code, allowing calls to foreign functions and memory manipulation.

Example of native function call:

import jdk.incubator.foreign.*;
public class NativeFunctionExample {
    public static void main(String[] args) {
        try (MemorySegment segment = MemorySegment.allocateNative(4)) {
            // ...
        }
    }
}

5. Region Pinning for G1

This performance update in the Garbage Collector aims to optimize resource allocation and reduce memory and CPU overhead.

and more…

Java 21 (LTS – Long Term Support version) new features

1. Sequenced Collections

Sequenced Collections provide a more efficient way to handle concurrent read and write operations on ordered data collections. This ensures data consistency and improves performance.

2. Generational ZGC

The Generational ZGC is a new Garbage Collector (GC) model that enhances the performance of applications requiring memory and CPU usage. It aims to reduce resource allocation risks.

3. Record Patterns

Record Patterns allow expressing patterns for functional programming, similar to languages like Kotlin, Rust, or C#. This facilitates the creation of immutable data objects.

and many more…

You can check out all new changes from Java 22 directly on Oracle website clicking here

I’ll be writing about each one of these features in separated posts here eventually with practical test case scenarios and with a Git repo to follow along, but we’ll be covering this as well in this live, so you’re more than welcome to join us live or even watch it’s recording after it’s complete. This is knowledge that always add a tool in your toolbox as a engineer.

And don’t forget to subscribe to be the first to receive everything about these new series of posts, new lives and the technology trends!

See you in the next post!