Java 23: The New Planned Features Coming Soon

Java 23: The New Planned Features Coming Soon
Photo by 🇸🇮 Janko Ferlič / Unsplash

The newest planned version of Java has now been announced. The next release is version 23 of the Java Development Kit. The release is planned for September 19th, 2024 and will include preview feature focusing on the integration of primitive types in patterns, instanceof, and switch statements. This is the same feature that is in preview with Java Development Kit 22 that was just released last week.

Need to reference to catch up on the last release of Java? Read our article about it right below!

A Look into Java 22’s New Features and Security Enhancements
Java Development Kit 22 – the next major version of Java Standard Edition – is now fully available as a production release! The newest version includes 12 features and major security enhancements. You are able to download it from oracle.com right now. This version is a short-term release that will receive

Class-File API

The introduction of a more refined class-file API marks an evolution from it's preview that was released in JDK 22. The API offer a standardized approach for parsing, generating, and modifying Java class files in alignment with the class file format stipulated by the Java Virtual Machine Specification. One of the pivotal goals is to transition JDK components away from the internal use of the third-party ASM Library towards this standard API. Enhancements in this iteration focus on simplifying the CodeBuilder class, which is instrumental in bytecode generation, by streamlining factory methods for bytecode instructions into low-level factories, mid-level factories, and high-level builders for basic blocks.

ClassFile classFile = new ClassFile("MyClass");
MethodBuilder mb = classFile.addMethod(ACC_PUBLIC, "myMethod", "()V");
mb.getInstructionFactory()
  .ldc("Hello, world!")
  .invokevirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V");
classFile.writeToFile("MyClass.class");

Privative Types in Patterns

Another noteworthy feature earmarked for JDK 23 is the augmentation of pattern matching to incorporate primitive types in instanceof and switch statements. This expansion aims at broadening the utility and applicability of pattern matching by enabling primitive type patterns across all contexts, thereby simplifying type checking and casting while mitigating the risk of information loss.

Object obj = 10;
if (obj instanceof Integer i) {
    System.out.println("Integer value: " + i);
}

int number = 5;
switch (number) {
    case int i && i > 0 -> System.out.println("Positive number: " + i);
    default -> System.out.println("Not a positive integer");
}

Oracle has also unveiled its roadmap for Java enhancements in 2024. The agenda includes leveraging OpenJDK projects such as Amber, Babylon, and Valhalla. These projects are set to focus on smaller, productivity-boosting features, extending Java to accommodate foreign programming models like GPUs, and refining the Java object model with value objects to alleviate performance bottlenecks.

Statements Before _super()

One of the proposed enhancements is the ability to execute statements before invoking a superclass constructor. This change would provide developers with greater flexibility in initializing an object before passing control to a superclass constructor.

class MyBaseClass {
    MyBaseClass(String message) {
        System.out.println(message);
    }
}

class MyDerivedClass extends MyBaseClass {
    MyDerivedClass() {
        // Proposed JDK 23 feature: execute statement before super
        String message = prepareMessage();
        super(message); // Pass the prepared message to the superclass constructor
    }
    
    private String prepareMessage() {
        return "Hello from MyDerivedClass";
    }
}

String Templates

Another promising addition is the introduction of string templates, which would simplify the creation of strings that incorporate runtime-computed values, making code more readable and easier to maintain.

String userName = "John";
int age = 30;
// Proposed JDK 23 feature: String templates
String greeting = `Hello, ${userName}, you are ${age} years old.`;
System.out.println(greeting);

Stream Gatherers

Stream gatherers are expected to enhance the flexibility and expressiveness of stream pipelines. This feature would make it easier to collect elements from a stream into collections or other data structures, thereby simplifying the code.

List<String> names = List.of("John", "Doe", "Jane", "Doe");
Map<String, Long> nameFrequency = names.stream()
    .collect(groupingBy(name -> name, counting())); // Stream gatherer in action
System.out.println(nameFrequency);

Scoped Values

Scoped values could revolutionize the way immutable data is shared within and across threads, enhancing the efficiency of concurrency in Java applications.

Scoped<String> userId = Scoped.valueOf("USER123");
// Access the scoped value in the current thread or pass it to another thread
System.out.println(userId.get());

Vector API

The evolution of the vector API to further stages signifies Java's commitment to leveraging hardware capabilities for performance optimization. This API facilitates expressing vector computations, which are then compiled to optimal vector instructions on supported CPUs, offering a significant performance boost for computational-heavy applications.

FloatVector a = FloatVector.fromArray(FloatVector.SPECIES_256, new float[] {1.0f, 2.0f, 3.0f, 4.0f}, 0);
FloatVector b = FloatVector.fromArray(FloatVector.SPECIES_256, new float[] {5.0f, 6.0f, 7.0f, 8.0f}, 0);
FloatVector c = a.add(b);
c.intoArray(new float[4], 0); // The result is [6.0, 8.0, 10.0, 12.0]

The Conclusion

With what Oracle is laying out for Java in 2024, they are focusing on enhancing the language's productivity, performance, and versatility. The focus on projects like Amber, Babylon, and Valhalla has an ambitious agenda to make Java more expressive and efficient – mainly in areas where it has historically faced challenges, such as value-based programming and integration with non-Java ecosystems.

As the newest release shapes up, it's evident that Java's evolution is a testament to its resilience and adaptability. These advancements not only promise to enrich the Java ecosystem but also ensure Java remains a top choice for modern software development, catering to the ever-changing demands of developers and the industry at large.


Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Implementing RSA in Python from Scratch as your next choice.
Implementing RSA in Python from Scratch
This is a guide to implementing RSA encryption in python from scratch. The article goes over the math and has code examples.

The CoderOasis Community

Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!
Join the CoderOasis.com Discord Server!
CoderOasis offers technology news articles about programming, security, web development, Linux, systems admin, and more. | 112 members
CoderOasis Forums
CoderOasis Community Forums where our members can have a place to discuss technology together and share resources with each other.