Here are some simple improvements that Java 25 brings to your everyday code.
Hello World simplified
Since the release of the C programming language, printing "Hello world!" is the first task a programmer does in a language. And Java did not shine at this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
We tell our students it's magical incantation that they'll have to live with for a while and that all will become clear in due time.
At last Java 25 brings this down to the essentials:
void main() {
IO.println("Hello, World!");
}
If I'm counting well, we go down from 89 characters to 40 characters, That's less than half of the code. Additionally the code is more intuitive to understand.
This feature is called compact source files. The above improvement uses
a launchable main method. The method automatically resides in a top level class in the unnamed package
A new Input/Output class
Less imports
void main() {
List<String> ducklings = List.of("Huey", "Dewey", "Louis");
IO.print("Hello " + ducklings);
}
We're all used to not having to import java.lang.String because Java automatically imports java.lang.*.
Java now automatically imports the java.base module.
Importing a module implies importing all public top level classes and interfaces in packages exported by that module.
The top level packages are defined in the module definitions, which exist since Java 9.
One of the packages exported by the java.base is java.util.*. Hence we do not need
import java.util.List;
for the code above to run anymore.
Additionally a program can now import modules without having to be a module itself,
making modules more accessible to developers who never dabbled with them themselves.
You import modules (here java.util.desktop) using
import module java.util.desktop;
No comments:
Post a Comment