Return to site

☕2️⃣3️⃣ Java 23 shorter println

· java23,java

With JEP 477, we're finally getting a shorter println method!

void main() { println("Hello, World!");
}

Checkout A N M Bazlur Rahman's latest infoQ item:  

The article by A N M Bazlur Rahman discusses JEP 477, which aims to simplify Java for beginners by introducing features that allow writing programs without understanding complex constructs. Key changes include:

  • Implicitly declared classes: Eliminates the need for explicit class declarations.
  • Instance main methods: main methods can be instance methods and don’t need to be static or public.

The classic “Hello, World!” program can now be written more simply as:

void main() { println("Hello, World!");
}

or even using a field:

String greeting = "Hello, World!";
void main() {
System.out.println(greeting);
}

The JEP also introduces automatic imports from java.base module, reducing the need for explicit import statements. This is part of the ongoing effort to make Java more accessible and maintain its robustness for complex applications. The features are available in JDK 23 with the --enable-preview flag. The proposal is a continuation of the work from previous JEPs and is based on community feedback. It’s designed to help new programmers start with simpler code and gradually learn more complex features.