Java 23 arrived in September 2023 with a range of new features and improvements. While it may not be a Long-Term Support (LTS) release, it offers some exciting additions worth exploring. In this blog post, we’ll dive into the full release features of Java 23, providing clear explanations and practical examples, and then take a look at the incubator feature.
Full Release Features
1. Simplified Module Imports (JEP 476)
Tired of importing individual classes from a module? Java 23 simplifies this process with Module Import Declarations. Now, you can import all public top-level classes and interfaces from a module with a single statement.
Example:
import module java.util; // Imports all public classes from java.util
public class ModuleImportExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names);
}
}
This feature reduces verbosity and makes your code cleaner, especially when working with large modules.
2. Flexible Constructor Bodies (JEP 465)
Java 23 introduces more flexibility in how you write constructors. Previously, any statements within a constructor had to appear after the this()
or super()
call. Now, you can include statements before these calls, as long as they don’t reference the instance being created.
Example:
public class FlexibleConstructorExample {
private final String name;
public FlexibleConstructorExample(String name) {
System.out.println("Initializing..."); // Allowed before this()
this.name = name;
}
public static void main(String[] args) {
new FlexibleConstructorExample("John Doe");
}
}
This allows for more natural placement of logic within constructors and reduces the need for auxiliary methods or complex constructor chaining.
3. Generational ZGC (JEP 481)
Garbage collection gets a performance boost in Java 23 with Generational ZGC. This enhancement introduces generational collection to the Z Garbage Collector, improving application throughput and reducing memory footprint. By separating objects into young and old generations, ZGC can perform more frequent garbage collection on the young generation, where most objects die quickly, leading to better performance.
This feature is enabled by default, so you can benefit from it without any code changes.
Incubator Feature
4. Foreign Function & Memory API (JEP 483)
The Foreign Function & Memory API (FFM API) allows Java programs to interact with code and data outside the Java runtime. This means you can call native libraries (like C libraries) and access native memory without the fragility and dangers of JNI.
This is still an incubator feature, meaning it’s not finalized and may change in future releases. However, it offers a powerful way to integrate Java with other systems.
Example:
import jdk.incubator.foreign.*;
public class NativeCallExample {
public static void main(String[] args) throws Throwable {
// Load the C library
LibraryLookup libc = LibraryLookup.ofDefault();
MethodHandle strlen = libc.lookup("strlen").get(FunctionDescriptor.of(long.class, ValueLayout.ADDRESS));
// Allocate off-heap memory and write a string to it
MemorySegment str = MemorySegment.allocateNative(CLinker.toCString("Hello, FFM!"));
// Call the strlen function from the C library
long len = (long) strlen.invokeExact(str);
System.out.println("Length of string: " + len);
}
}
This example demonstrates how to use the FFM API to call the strlen
function from the C standard library.
In Summary
Java 23 brings valuable features that enhance developer productivity and application performance. Simplified module imports reduce code verbosity, flexible constructor bodies offer more coding freedom, generational ZGC boosts garbage collection efficiency, and the FFM API provides powerful integration with native code. Be sure to explore these features and leverage them in your Java projects.
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.