top of page
Search

Java 21 is a Major LTS Release

  • schick09
  • Jul 28
  • 3 min read

Java 21 is a major milestone—it's the next Long-Term Support (LTS) release following Java 17, and it finalizes several powerful features that were in preview or incubation in earlier versions.


Java 21, released in September 2023, is a Long-Term Support (LTS) version, making it a critical release for organizations and developers planning to upgrade from Java 17 or earlier.


This release finalizes a number of key innovations from the past several versions, especially in areas of virtual threads, pattern matching, record deconstruction, and native interop. It also marks the official arrival of several Project Loom and Project Panama milestones.


Whether you’re building microservices, low-latency systems, or modern desktop apps, Java 21 offers cleaner syntax, higher performance, and more powerful tools out of the box.


Let’s take a detailed look at what’s new.



✅ Finalized Features in Java 21


1. Virtual Threads — JEP 444


Virtual threads are now officially part of the Java platform. These lightweight threads make it easy to write scalable, concurrent applications without the complexity of thread pools or async frameworks.


Why it matters:

  • Each task gets its own thread without the cost of platform threads

  • Works with existing APIs like Thread, ExecutorService, etc.

  • Perfect for high-throughput, IO-bound workloads like servers


For example:


Thread.startVirtualThread(() -> {

    System.out.println("Running in a virtual thread");

});




2. Pattern Matching for switch — JEP 441


This powerful enhancement lets you use patterns directly in switch statements and expressions, improving readability and eliminating boilerplate.


For example:


static String handle(Object o) {

    return switch (o) {

        case String s when s.length() > 5 -> "Long string";

        case String s -> "Short string";

        case Integer i -> "Integer: " + i;

        case null -> "Null!";

        default -> "Something else";

    };

}




3. Record Patterns — JEP 440


Record Patterns make it easier to destructure data directly in conditionals and switch statements, particularly useful when working with immutable data.


For example:


record Point(int x, int y) {}


static void print(Object obj) {

    if (obj instanceof Point(int x, int y)) {

        System.out.println("x = " + x + ", y = " + y);

    }

}




4. String Templates (Preview) — JEP 430


This new feature introduces template expressions for safer and more readable string interpolation. No more messy String.format() or + operations.


For example:


String name = "David";

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


  • Secure and efficient

  • Can be customized via processors for SQL, JSON, HTML, etc.






🧪 Incubator and Preview Features


5. Structured Concurrency (Preview) — JEP 453


Structured concurrency is finally available as a preview in Java 21. It treats related tasks as a single unit of work, simplifying error handling, cancellation, and lifecycle management.


For example:


try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {

    Future<String> user = scope.fork(() -> fetchUser());

    Future<String> order = scope.fork(() -> fetchOrder());

    scope.join();

    scope.throwIfFailed();

    return user.result() + order.result();

}




6. Unnamed Classes and Instance main Methods (Preview) — JEP 445


This feature reduces boilerplate for writing small Java programs by allowing unnamed classes and simplified main() methods.


For example:


void main() {

    System.out.println("Hello, World!");

}


  • Great for scripting, education, and fast prototyping





7. Foreign Function & Memory API — JEP 442


This modern API replaces JNI for working with native libraries and off-heap memory in a safe, efficient, and idiomatic way.


For example:


Linker linker = Linker.nativeLinker();

SymbolLookup lookup = SymbolLookup.systemLookup();

FunctionDescriptor strlen = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS);


  • Great for calling C libraries

  • Easier than JNI

  • Safer memory access patterns





🧹 Smaller Enhancements


  • Deprecation of the finalize() method — JEP 421 (from Java 18) continues to be enforced

  • Performance and stability improvements across the JVM

  • Continued refinements to preview features like Pattern Matching and the Vector API



✅ Should You Upgrade to Java 21?


Java 21 is an LTS release, meaning it will be supported for years. It's the best time to upgrade if you're still on Java 11 or Java 17.


Upgrade now if:

  • You want to adopt virtual threads for simpler concurrency

  • You're modernizing legacy code and want clean, concise syntax

  • You’re migrating away from JNI to safer native interop

  • You want access to long-term updates and support



📝 Final Thoughts


Java 21 isn't just another release—it's the culmination of years of work across Project Loom, Amber, and Panama. With the introduction of virtual threads, pattern matching, record destructuring, and preview support for string templates and structured concurrency, Java is leaner, more expressive, and more performant than ever.



Coming Up Next: Java 22 and Beyond

Java 22 and 23 are expected to build on this solid foundation. If you're interested in what's next—like finalizing string templates and structured concurrency—stay tuned for our coverage of Java 22!


ree

 
 
 

Comments


Dave Schick Consulting

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

bottom of page