5 Java 9 Features you might not know yet

Last Updated:  July 14, 2020 | Published: April 9, 2019

Java SE 9 has been released in September 2017, three years after the Java SE 8 release from Oracle. The adoption of the Jigsaw module system since then is moderate in the Java community. Some developers skip the non-LTS releases (Java SE 9 is a non-LTS release) and upgrade to Java SE 11 directly. With three years of development for Java 9, there were a lot of useful Java API changes besides the Jigsaw module system. In this blog post, you'll find five useful Java 9 features you might not know yet.

Collection factory methods with Java 9

With Java 9 we now have factory methods to create collections (List,Set, Map). These .of() methods create immutable collections, so adding a new value afterward will throw an exception.

This new feature might reduce boilerplate code as prior to Java 9 a collection was first initialized and all values were added afterward.

Interaction with OS processes with ProcessHandle

Prior to Java 9 the interaction with OS process to get e.g. the PID, the command, general information was cumbersome. With Java 9 we know to get a new interface ProcessHandle for more convenient process interaction.

Update to Streams

The Stream<T> interface has now two new methods: .takeWhile(Predicate<? super T> predicate) and .dropWhile(Predicate<? super T> predicate) to control which values of the stream should be processed or dropped until a specific condition is met.

Objects.requireNonNull updates in Java 9

For checking preconditions at the beginning of a method Objects.requireNonNull is really useful. With Java 9 this utility check has been improved with two new methods: .requireNonNullElse(T obj, T defaultObj) and .requireNonNullElseGet(T obj, Supplier<? extends T> supplier) which you behave as the methods from Optional<T>. With these methods, you can now define default values instead of throwing an exception if a value is null:

JShell feature since Java 9

Java 9 introduces a REPL (read-eval-print-loop) tool for Java JShell. With Java on your PATH, you can enter the JShell with the jshell command. With this Java shell, you can write Java code without explicit compiling and writing .java files. You can use this feature for quick evaluations or simple showcases for the Java language. With TAB you get tab-completion like coding in an IDE and the semicolon is optional at the end of a line:

If you don't specify a variable name, the JShell will assign it automatically to a variable with $ as a prefix.

Additional useful commands within the JShell are:

  • /exit or /ex leave the JShell
  • /list list all the previous commands
  • /{NUMBER} re-execute a previous command
  • /! execute the previous command
  • /vars list all available variables
  • /methods list all available methods
  • /edit open a UI editor

For a Java 9 deep-dive to the Jigsaw module system I can highly recommend the following books: Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications or Java 9 – Die Neuerungen (For Germans).

For essential Java developer resources in general, take a look at this list.

The codebase is available on GitHub.

Have fun with these Java 9 features,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>