DEV Community

Cover image for Simplifying Java for Learners: JDK 23 Preview Features
MyExamCloud
MyExamCloud

Posted on

Simplifying Java for Learners: JDK 23 Preview Features

Java programming can be challenging for beginners, especially when it comes to dealing with modules and importing multiple packages. However, the latest JDK 23 has introduced two preview features that can simplify coding for Java learners: module import and implicitly declared classes.

JEP 476, also known as the "Module Import Declarations (Preview)", allows developers to import entire modules instead of having to explicitly import individual packages. This means that instead of writing multiple import statements for different packages, you can simply import one module and have access to all the packages within it.

For example, instead of writing:

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
Enter fullscreen mode Exit fullscreen mode

or:

import java.util.*;
import java.util.function.*;
import java.util.stream.*;
Enter fullscreen mode Exit fullscreen mode

we can simply write:

import module java.base;
Enter fullscreen mode Exit fullscreen mode

This makes the code cleaner, easier to read, and less prone to errors. Beginners can benefit from this feature as it gives them a better understanding of how modules work in Java without getting overwhelmed by multiple import statements.

JEP 477, also known as the "Implicitly Declared Classes and Instance Main Methods (Third Preview)", is another feature that simplifies Java coding for learners. It allows programmers to write simple single-class programs without the need for advanced features. These programs can then be extended as their knowledge and skills grow.

But what are implicitly declared classes and instance main methods?

Implicitly declared classes are classes that do not have a name, reside in an unnamed package, and can only be accessed through their default zero-parameter constructor. On the other hand, instance main methods are non-static and do not require a public access modifier or a String[] parameter. This means that beginners can write basic programs without worrying about these advanced concepts.

In the previous JDK, this feature allowed for only one static method to be imported from the java.io.IO class. However, with the third preview of JEP 477, this has been extended to three static methods: println(), print(), and readln(). These methods are automatically imported, making it easier for beginners to write interactive programs without having to deal with System.in and System.out.

Additionally, the java.base module is now automatically imported, giving beginners access to commonly used packages and their APIs. This eliminates the need for explicit imports and allows for more streamlined and beginner-friendly coding.

Example of an implicitly declared class:

// just save as Test.java, now Test is your class name
// javac --release 23 --enable-preview Test.java
// java  --enable-preview Test

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

In this example, the class is not given a name and does not belong to any package. It also contains a main method, making it a standalone program.

Both of these features make it easier for beginners to write simple programs without getting bogged down by advanced concepts and features.

In conclusion, the latest preview features in JDK 23, JEP 476 and JEP 477, make learning Java easier for beginners by simplifying module imports and implicitly declared classes. They encourage a more streamlined and cleaner coding experience, allowing learners to focus on building their understanding of the language without getting overwhelmed by advanced concepts.

Improve your Java knowledge by accessing the newest version of Java Certification Practice Tests from MyExamCloud.

Top comments (0)