Sitemap

Java 21 makes code in Java easier

4 min readJun 30, 2023

--

Press enter or click to view image in full size

First, Look at code below.

class FirstJavaApp{

public static void main(String[] args){
System.out.println("Hello World!");
}

}

Every java programmer knows what it is. Because most of Java programmers, start their Java learning journey from this code sample. But in that time it’s bit complicated thing to understand what are those words public, static, void, String[] args.

When we study Java as beginner we have to to learn about main method. It’s the entry point for executing a Java program. This has been unchanged for almost more than two decades. But now it’s an biggest change in hello world program of Java new comers in JDK 21. Look at the following code.

void main(){

System.out.println("Hello World!");

}

Will this work? 😳 first question. It’s not applicable with currently officially released JDK versions. But it works with JDK21 which scheduled to released in this September. (https://www.java.com/releases/matrix/)

When check this new main method, It’s very simple and clear looking. It could be very easy to teachers to teach and test for beginner students. Because it hasn’t those modifiers, parameters and class block.

But according to point of my view older main method is meaningful than this one. Because it has the good structure and shows all modifiers.

In this new main method JVM identifying as main method and behind the scenes JVM knows it’s an public and static method. JVM should get the class name from the file name.

Let’s test it 😊

If you like to test it out, continue reading.

  1. First thing is download JDK21. It’s not officially released, So we can use early access build from here (https://jdk.java.net/21/).

Click on archive file suitable for your operation system.

2. Extract file to your preferred location. Extracted file name should be jdk-21.jdk.

Extracted folder

3. Move it to JavaVirtualMachines folder.

Path to JavaVirtualMachines folder in different OS as. below

Mac OS : /Library/Java/JavaVirtualMachines

Windows: C:\Program Files\Java

Linux : /usr/local (Tested in Ubuntu Linux, Could be change according to your linux distro)

4. Set JDK path

If you are a Mac user you can set path by simply running following command. (If you want to roll back to your previous JDK version simply replace “21” to your previous version number. eg: if it 11 replace 11 with 21, if JDK 8, replace 1.8 with 21)

Get Senura Chamod’s stories in your inbox

Join Medium for free to get updates from this writer.

*It’s recommended to rollback after testing. As a EA build it could has bugs.

export JAVA_HOME=`/usr/libexec/java_home -v 21`

5. To Confirm JDK version, run following command in terminal.

java -version

6. If output as below (21-ea), We can continue.

Press enter or click to view image in full size
Java version check

In this case we cannot user our favourite IDEs to test it. Because they are not updated to this Java version which has to be released in the future.

Press enter or click to view image in full size
Netbeans IDE
Press enter or click to view image in full size
Got an Error when try to run it.
Press enter or click to view image in full size
IntelliJ IDEA IDE

So we have to use normal text editor and terminal to compile and run.

As a mac user I’m using Vim editor on terminal to create the file as below. You can use your note pad and edit save as a “.java” file.

Press enter or click to view image in full size
Press enter or click to view image in full size

Let’s compile it using javac command.

Press enter or click to view image in full size
It shows this error 😢.

In this case we have to enable jdk preview features to compile it. So we have to add “ — release 21 — enable-preview” phrase additionally to javac command like below.

javac --release 21 --enable-preview FirstJavaApp.java 
Press enter or click to view image in full size
Successfully compiled. 😋
Compiled file (.class file) with java file

Let’s run it. 😀

Press enter or click to view image in full size
Error when run it 🥲

In this case also we have to use “ — enable-preview” phrase to run it like below.

java --enable-preview FirstJavaApp

Done it. Got the output 😃

Press enter or click to view image in full size
Final Output.

So in future Java versions, we can create methods without class block (if java file has only one class). JVM get java file name as class name. This will be very helpful scenarios like when using Object Oriented Design patterns because some scenarios we have to create many java files for java classes.

--

--