Did Someone Say Java 9?

DZone 's Guide to

Did Someone Say Java 9?

Here's a breakdown of the most exciting features coming to Java 9, complete with explanations of how they'll help and sample code.

· Java Zone ·
Free Resource

In a previous blog, we discussed the features that were added in Java 8.

More than three years after the release of Java 8, the next version is now just around the corner, with a tentative release date of Sept. 21, 2017.


You may have heard about Java 9’s module system, but there’s even more to this new version. In this blog, We will try to cover some of them.

One can download JDK 9 EA (Early Access) from here.



So, without wasting anymore time, let's begin! 

Java 9 REPL(JShell)

Oracle has introduced a new tool called “JShell.” It stands for Java Shell and is also known as REPL (Read Evaluate Print Loop). It is used to execute and test any Java construct, like class, interface, enum, object, statements, etc., very easily. Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. The REPL/JShell looks like:

 Welcome to JShell -- Version 9-ea
 For an introduction type: /help intro 

jshell> System.out.println("Hi All")
 Hi All

No More `public static void main(String[] args)`. Cool, right? 

HTTP 2.0 Client

Java 9 will have full support for HTTP 2.0 and features a new HTTP client for Java that will replace HttpURLConnection — and also implements HTTP 2.0 and websockets.

The new API is located under the java.net.http package.

This API uses the Builder pattern, which makes it really easy for quick use :

URI newURI = new URI("http://localhost:8080");
HttpRequest request = HttpRequest.create(newURI).GET();
HttpResponse response = request.response();
String responseBody = response.body(HttpResponse.asString());

Java 9 Modularity (Project Jigsaw)

One of the big changes in Java 9 features is the module system. Oracle is going to introduce the following features as part of Project Jigsaw.

  • Modular JDK
  • Modular Java source code
  • Modular run-time images
  • Encapsulate internal Java APIs
  • Java Platform Module System

One of the main motivations here is to provide modular JVM, which can run on devices with a lot less available memory. The JVM could run with only those modules and APIs which are required by the application.

The module system will look somewhat like :

module java9 {


Modular JAR files contain an additional module descriptor. In this module descriptor, dependencies on other modules are expressed through`requires` statements. Additionally, `exports` statements control which packages are accessible to other modules.

You can explore more about Jigsaw here.

Process API Improvements

Java SE 9 is coming with some improvements in the Process API. They added a couple of new classes and methods to ease the controlling and managing of OS processes.

Two new interfaces in the Process API:

  • java.lang.ProcessHandle
  • java.lang.ProcessHandle.Info

This update will extend Java’s ability to interact with the operating system: New direct methods to handle PIDs, process names and states, and the ability to enumerate JVMs, processes, and more. For example, the code for retrieving Linux PIDs will look like:

System.out.println(" Required PID is " + Process.getCurrentPid());

Lightweight JSON API

Java 9 comes with a lightweight API for consuming and generating documents and data streams via the JavaScript Object Notation data interchange format, which is based on a subset of JavaScript and serves as an alternative to XML.

The main goals of this API will be as follows:

  • Parsing and generation of JSON.
  • Functionality to meet the needs of Java developers using JSON.
  • Parsing APIs, allowing a choice of parsing token streams, event (includes document hierarchy context) streams, or immutable tree representation views of JSON documents and data streams.
  • Useful API subset for compact profiles and Java ME.
  • Immutable value tree construction using a Builder-style API.
  • Generator-style API for JSON data stream output and for JSON “literals”.
  • A transformer API, which takes an existing value tree as an input and produces a new value tree as a result.

It is expected that this JEP will be delivered as a set of new sub-packages in the java.util hierarchy and will consist of at least four modules: events, streams, trees, and generators. No existing modules, packages, or classes are expected to be modified. The JSON API is not expected to have any dependencies that are not already part of the Java base module.

Smart Java Compilation (Phase 2)

The Smart Java Compilation Project is intended to improve the sjavac (Java programming language compiler) tool, enabling it to be used by default in the JDK build and also generalizing it so it can be used to build large projects other than the JDK.

Due to the various issues relating to stability and portability, sjavac is not used by default in the JDK build scripts. The first goal of this JEP is to resolve these issues. This involves making sure the tool produces reliable results on all software/hardware configurations at all times.

The overarching goal is to improve the quality of sjavac to the point where it can serve as a general purpose javac wrapper able to compile large arbitrary Java projects.

Private Interface Methods

In Java 8, we can provide method implementation in Interfaces using Default and Static methods. However, we cannot create private methods in Interfaces.

To avoid redundant code and add more reusability, Oracle is going to introduce private methods in Java SE 9 Interfaces. From Java SE 9 onward, we can write private and private static methods in an interface using the ‘private’ keyword.

A private method can be static so it belongs to an interface. A private method without static can be called on an instance of the interface, but only within the interface.

public interface Employee{

    private Long createEmployeeID(){
        // Method implementation goes here.

    private static void displayEmployeeDetails(){
        // Method implementation goes here.

If you’re evolving APIs with default methods, private interface methods can be helpful in structuring their implementation.

Reactive Streams

Nowadays, Reactive programming has become very popular in developing applications to get some beautiful benefits. Scala, Play, Akka, etc. have already integrated Reactive Streams and are getting many benefits. Oracle is also introducing a new Reactive Streams API in Java SE 9.

The class java.util.concurrent.Flow encloses the following four interfaces:

These interfaces support the Reactive Streams publish-subscribe framework. Java 9 also provides a utility class: SubmissionPublisher. A publisher produces items that are consumed by a number of subscribers. And a subscriber is managed by a subscription. A subscription links the publisher and subscriber.

Мulti-Resolution Image API

In Java SE 9, Oracle is going to introduce a new Мulti-Resolution Image API. The important interface in this API is MultiResolutionImage .

MultiResolutionImage encapsulates a set of images with different heights and widths (that is, different resolutions) and allows us to query them with our requirements.

Basically, this new API allows a set of images with different resolutions (width and height) to be encapsulated into one single image.

The new API, which is defined in the java.awt.image package, can help us:

  • Encapsulate many images with different resolutions into an image as its variants.
  • Get all variants in the image.
  • Get a resolution-specific image variant – the best variant to represent the logical image at the indicated size based on a given DPI metric.

Java 9 provides a MultiResolutionImage Interface with two important methods:

  • getResolutionVariant(): returns an image

  • getResolutionVariants(): returns a list of images

package java.awt.image;

public interface MultiResolutionImage {

    Image getResolutionVariant(double destImageWidth, double destImageHeight);
    public List getResolutionVariants();

Apart from these features, There are loads of new features/improvements in store with the release of Java 9 like:

  • GC (Garbage Collector) improvements
  • HTML5 Javadocs
  • Stream API improvements
  • Enhanced @Deprecated annotations
  • Optional class improvements
  • CompletableFuture API Improvements.

Well, all said, all these features are expected to make their way into the Java 9 update, but we can say nothing is ever guaranteed when it comes to Java. So, let's wait and see what Java 9 has in store for us!

java, java 9, jshell, project jigsaw, tutorial

Published at DZone with permission of Anmol Sarna , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}