DEV Community

Cover image for JDK 24 New Features Every Java Developer Must Know
MyExamCloud
MyExamCloud

Posted on

JDK 24 New Features Every Java Developer Must Know

Java Development Kit (JDK) 24 brings a new set of powerful features aimed at improving performance, code readability, and developer experience. In this article, we will explore the major enhancements in JDK 24 with brief coding examples to demonstrate their practical applications.

1. Primitive Types in Patterns, instanceof, and switch (JEP 488)

Overview

JDK 24 extends pattern matching by allowing primitive types in all pattern contexts. The instanceof and switch statements now support primitive types, making the language more expressive and eliminating unnecessary casts.

Example: instanceof with Primitive Types

public class InstanceofExample {
    public static void main(String[] args) {
        Object value = 42;
        if (value instanceof int i) {
            System.out.println("Integer value: " + i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Example: switch with Primitive Type Patterns

public class SwitchExample {
    public static void main(String[] args) {
        int status = 2;
        String message = switch (status) {
            case 0 -> "OK";
            case 1 -> "Warning";
            case 2 -> "Error";
            case int i -> "Unknown status: " + i;
        };
        System.out.println(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Flexible Constructor Bodies (JEP 492)

Overview

JDK 24 allows statements to appear before explicit constructor invocations (super() or this()). This improvement enables better initialization of fields before invoking another constructor.

Example: Validating Arguments Before Calling Superclass Constructor

class Person {
    String name;
    Person(String name) {
        this.name = name;
    }
}

class Employee extends Person {
    Employee(String name) {
        if (name == null || name.isBlank()) {
            throw new IllegalArgumentException("Name cannot be empty");
        }
        super(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Module Import Declarations (JEP 494)

Overview

A new import module syntax simplifies modular programming by allowing all exported packages of a module to be imported in one statement.

Example: Importing a Module

import module java.sql;

public class ModuleImportExample {
    public static void main(String[] args) {
        System.out.println("Module import works!");
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Simple Source Files and Instance Main Methods (JEP 495)

Overview

JDK 24 allows writing Java programs without class declarations. It also introduces instance main methods, making Java more beginner-friendly and reducing boilerplate code.

Example: Writing a Simple Java Program Without a Class

void main() {
    System.out.println("Hello, World!");
}
Enter fullscreen mode Exit fullscreen mode

Run the program with:

java HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

Example: Instance main Method

class HelloWorld {
    void main() {
        System.out.println("Instance main method works!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Run with:

java HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

Conclusion

JDK 24 introduces significant enhancements that simplify coding, improve readability, and enhance modular development. Features like primitive type patterns, flexible constructor bodies, module imports, and simplified source files make Java even more efficient and enjoyable to work with. Java developers should start experimenting with these features to stay ahead in the evolving ecosystem.

Top comments (1)

Collapse
 
xzel profile image
Axel Howind

A word of caution: these features are still in preview and have to be explicitly enabled using --enable-preview, and more importantly, they might change or be removed in the next version as has recently happened with string templates.