Java 9 New Features

Bulruno
4 min readDec 22, 2020

--

1. The Java Platform module system

2. Linking

3. JShell: the interactive Java REPL

4. Improved Javadoc

5. Collection factory methods

6. Stream API improvements

7. Private interface methods

8. HTTP/2

9. Multi-release JARs

1. The Java Platform module system

The defining feature of Java 9 is an all-new module system. When codebases grow larger, the odds of creating complicated, tangled ‘spaghetti code’ increase exponentially. There are two fundamental problems: It is hard to truly encapsulate code, and there is no notion of explicit dependencies between different parts (JAR files) of a system. Every public class can be accessed by any other public class on classpath, leading to inadvertent usage of classes that weren’t meant to be public API. Furthermore, classpath itself is problematic: How do you know whether all required JARs are there, or if there are duplicate entries. Module system addresses both issues.

Note that both modules contain packages that are encapsulated because they’re not exported. Nobody can accidentally use classes from those packages. The java platform itself has been modularized using its own module system as well. By encapsulating JDK Internal classes, the platfor mis more secure and evolving it becomes much easier.

2. Linking

When you have modules with explicit dependencies, and modularized JDK, new possibilities arise. Your application modules now state their dependencies on their application modules and on the modules it uses from JDK. Why not use that information to create minimal runtime environment, containing just those modules necessary to run your application? That’s made possible with new jlink tool in java9. Instead of shipping your app with a fully loaded JDK installation, you can create a minimal runtime image optimized for you application.

3. JShell: the interactive Java REPL

Many languages already feature Read-Eval-Print-Loop, and Java now joins this club. You can launch jshell from the console and directly start typing and executing Java code. The immediate feedback of jshell makes it great tool to explore APIs and try out language features.

Testing Java regular expression is a great example of how jshell can make your life easier. The interactive shell also makes for a great teaching environment and productivity boost.

4. Improved Javadoc

Sometimes its the little things that can make a big difference. Did you use Google all the time to find the right javadoc pages, just like me? Thats no longer necessary. Javadoc now includes search right in the API documentation itself. As an added bonus, the Javadoc output is not HTML5 compliant. Also, you’ll notice that every javadoc page includes informatio non which JDK module the class or interface comes from.

5. Collection factory methods

Often you want to create a collection in your code and directly populate it with some elements. That leads to repititive code where you instantiate the collection, followed by several ‘add’ calls. With Java 9, several so-called collection

Besides being shorter and nicer to read, these methods also relieve you from having to pick a specific collection implementation.

6. Stream API improvements

The Streams API is arguably one of the best improvements to the Java standard library in a long time. It allows you to create declarative pipelines of transformations on collections. With Java 9, this only gets better. There are four new methods added to the Stream interface: dropWhile, takeWhile, ofNullable. The iterate method gets a new overload, allowing you to provide a Predicate on when to stop iterating.

7. Private interface methods

Java 8 brought us default methods on interfaces. An interface can now contain behaviro instead of only method signatures. But what happens if you have several default methods on an interface with code that does almost same thing? Normally, you’d refactor those methods to call a private method containing the shared functionality. But default methods can’t be private. Creating another default method with shared code is not a solution, because this helper method becomes part of public API. With Java 9, you can add private helper methods to interfaces to solve the problem:

8. HTTP/2

A new way of performing HTTP calls arrives with Java 9. This much overdue replacement for the old ‘HTTPURLConnection’ API also supports WebSockets and HTTP/2 out of the box. One caveat: The new HttpClient API is delievered as a so-called _incubator module_ in Java 9.

Besides simple request/response model, HttpClient provides new APIs to deal with HTTP/2 features such as streams and server push.

9. Multi-release JARs

The least feature we’re highlighting is especially good news for library maintainers. When a new version of Java comes out, it takes years for all users of your library to switch to this new version. That means library has to be backward compatible with the oldest version of Java you want to support. That effectively means you won’t get to use new features of Java 9 in your library for long time. Fortunately, the multi-release Jar feature allows you to create alternate versions of classes that are only used when running library on a specified Java version.

In this case, multirelease.jar can be used on Java 9, where instead of the top-level multirelease.Helper class, the one under ‘META-INF/version/9’ is used. This Java 9-specific version of the class can use Java 9 features and libraries. At the same time, using this JAR on earlier Java versions still works, since older Java versions only see top-level helper class.

--

--