Isaac Tonyloi - SWE
Posted on September 19, 2024
Java 23 has officially been released! It’s a non-LTS (Long Term Support) release. Java 23 comes packed with exciting improvements, bug fixes, and also removed features and options that you may need to pay attention to.
Let’s dive into what’s new and how it builds on features from previous versions like JDK 21 and JDK 22.
Scoped Values: Building on JDK 22 and JDK 21 Previews
One of the most anticipated features in JDK 23 is Scoped Values (Preview). This feature, which was also previewed in both JDK 22 and JDK 21, simplifies the way methods share immutable data across threads and child threads. In previous Java versions, handling thread-local data could be cumbersome and memory-hungry. Scoped values change that by offering a more efficient, lower-overhead alternative.
What’s different in JDK 23? The ScopedValue.callWhere() method now uses a new functional interface, which makes it easier for the compiler to infer whether a checked exception might be thrown. This also led to the removal of the ScopedValue.getWhere() method, further streamlining the API.
This is a continuation of the improvements we saw in earlier releases, and it’s exciting to see scoped values evolving toward becoming a permanent feature.
Structured Concurrency: A Third Preview
Java’s concurrency model has always been powerful but tricky to manage. Enter Structured Concurrency (Preview), which first appeared in JDK 21 and has now reached its third preview in JDK 23. The idea here is to make concurrent programming more intuitive by grouping related tasks running in different threads into a single unit of work. This simplifies error handling and improves the reliability of cancellation processes.
In JDK 23, structured concurrency remains in preview, but it continues to make concurrency code easier to reason about and less error-prone. If you’ve worked with the preview in JDK 21 or JDK 22, you’ll appreciate how this feature has matured. It’s now better equipped to deal with common concurrency issues like thread leaks, making it easier to write reliable multithreaded code.
Flexible Constructor Bodies: Evolving Since JDK 22
Another welcome addition is Flexible Constructor Bodies (Preview). First previewed in JDK 22 as “statements before super()
”, this feature allows developers to write initialization code before invoking the superclass constructor. This seemingly small change can make constructor logic much clearer and prevent unnecessary object creation when validation or other setup logic fails.
In JDK 22, this feature was introduced to give developers more control over constructor logic, and the second preview in JDK 23 continues to refine that capability. By moving initialization logic ahead of the superclass invocation, developers can avoid messy auxiliary static methods and intermediate constructors that previously cluttered codebases.
Implicitly Declared Classes: Third Time’s a Charm
For those teaching Java or writing quick scripts, Implicitly Declared Classes and Instance Methods have been a game-changer since their introduction in JDK 21. This feature allows beginners to write simplified Java programs without needing to understand complex language features from the start.
In JDK 23, this feature gets some additional upgrades. Now, implicitly declared classes can automatically import three static methods for simple text I/O, and they will also import all public top-level classes and interfaces of the java.base
module on demand. These enhancements help make the learning curve even smoother for newcomers.
Module Import Declarations: A New Preview
One of the fresh preview features in JDK 23 is Module Import Declarations. This feature allows you to import all packages exported by a module with a single statement, simplifying the reuse of modular libraries. It’s especially useful for beginners who no longer need to memorize where packages are located in the hierarchy, and it reduces the clutter caused by multiple type-import-on-demand declarations.
While this is a brand-new feature in JDK 23, it shows great promise in making Java’s module system more accessible, particularly for those who are new to modular programming.
Markdown Documentation Comments: A New Era for Javadoc
Let’s be honest—writing JavaDoc with HTML and the @
tags can be painful. In JDK 23, the Markdown Documentation Comments (Preview) feature allows developers to write API documentation using Markdown, making it easier to create readable, well-formatted comments.
While this is a new feature in JDK 23, it’s something that should have been added years ago. Markdown is widely used in developer communities, and bringing it to JavaDoc will make writing and maintaining documentation far easier.
Z Garbage Collector (ZGC): Generational Mode by Default
If you’ve been working with the Z Garbage Collector (ZGC) since its introduction in JDK 15, you’ll be excited to know that in JDK 23, ZGC now defaults to generational mode (JEP 474). This switch improves application performance by focusing on frequent collection of short-lived objects. While non-generational ZGC is deprecated in this release, it’s a clear sign that the generational approach is the future of garbage collection in Java.
If you’re running on JDK 22 or earlier, moving to JDK 23 should bring noticeable performance improvements without much effort on your part.
Vector API: Still Incubating, But More Powerful
The Vector API, introduced in JDK 16, allows developers to perform vector computations that compile to optimal vector instructions at runtime. The API continues to evolve, and in JDK 23, we’re seeing the Eighth Incubator. This API is still incubating, meaning it’s not yet a finalized feature, but it’s inching closer to maturity.
For performance-critical applications, particularly those dealing with large datasets, the Vector API can provide massive speedups. If you’ve worked with it in previous incubations (like JDK 22), you’ll find that it’s even more stable now, but it’s still waiting for the final pieces to fall into place (likely tied to Project Valhalla).
Stream Gatherers: Second Preview
JDK 22 introduced Stream Gatherers (Preview), a feature that adds flexibility to the Stream API by allowing custom intermediate operations. In JDK 23, it’s back for a second preview. If you love using streams to process data, you’ll appreciate this feature’s ability to transform data pipelines in ways that were previously difficult to achieve.
Stream gatherers extend the power of streams, allowing developers to handle more complex scenarios with ease, particularly when working with infinite streams or custom transformations.
Class-File API: Second Preview
If you’re building tools that manipulate Java class files, the Class-File API (Preview) in JDK 23 is for you. First introduced in JDK 22, this API provides a standard way to parse and generate class files without needing third-party libraries like ASM. The second preview in JDK 23 refines this API, making it more streamlined and easier to work with.
For tool developers, this is a big deal. No more scrambling to update libraries every time the JVM spec changes—Java now has a first-party solution.
Wrapping Up: Should You Upgrade?
Java 23 may not be an LTS release, but it’s packed with exciting features that build on what JDK 21 and JDK 22 introduced. From scoped values and structured concurrency to a better garbage collector and improvements to streams and constructors, this release pushes Java development forward.
Posted on September 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.