What’s new in Java 9?

Mansoorali Shaikh
3 min readApr 21, 2020

--

Java 9 is the only publicly supported version, while after public support periods of older versions has ended, non-public updates have been issued for Java 7 and earlier

First of all, Java9 looks amazon, Its awesome. There have been lot of upgradations, which previously required third party applications to process certain features.

Let me simplify one thing and keep this article as generic as possible to all the levels of programmers. One need not be proficient in C /C++ to learn Java. Every language is unique and it applies the same to Java 9.

To be frank, a beginner to programming might not find anything ground breaking in Java9 but there are few which I’ll be describing here.

To begin with, if you haven’t downloaded the latest version of Java. Go to Oracle site and download it. Or click here .

There are many things added to Java 9. In this blog we shall understand the core features like Jshell and Process Handle Updates.

Jshell

So, what is Jshell? Java Shell or Jshell is a tool similar to IDLE of python or playground of swift. It is used to read - print – evaluate and loop your code.

It comes in handy as many trial versions of the code can be tested here.

These are not meant for writing the entire code because they are not saved but they are an amazing way to test the programs tests case.

Jshell is not that impressive but a great move by oracle.

Process Updates

Let us have a directed understanding. For example, if you have to find the process Id of your JVM you need to depend on third party modules and again all of which are platform specific.

Java.class.Object contains the process handle methods so we need even import a library.

Project Jigsaw. Why modules?

Module system is the fundamental Java 9 concept which completely will change the way we will be structuring our applications. In current environment even though your app requires only a tiny part of the Java SE library it has to be loaded entirely. However, this is to change. Thanks to changes in the next version of Java, to run applications on the client computer, you will not need to download all the heavy environment.

The main goal of Jigsaw is to reorganize existing JRE and JDK into interoperable modules, leading to getting rid of the rt.jar (Runtime JAR) files, which for many are an inexhaustible source of pain. This will make it easy to scale the JDK and JRE down to small computers. Modularity will also improve the security aspects. Java 9 will introduce clear boundaries between components and clearly define what needs to be inside the module and what can be seen outside.

By default, the new JDK will provide to the end users 92 modules. We can also create our own modules as shown below:

module com.montrosesoftware.utils {

requires com.google.guava; // dependent module name

exports com.montrosesoftware.utils; // exported package name

}

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:

IntStream.iterate(1, i -> i < 100, i -> i + 1).forEach(System.out::println);

The second argument is a lambda that returns true until the current element in the IntStream becomes 100. This simple example therefore prints the integers 1 until 99 on the console.

Besides these additions on Stream itself, the integration between Optional and Stream has been improved. It's now possible to turn an Optional object into a (possibly empty) Stream with the new `stream` method on Optional:

Stream<Integer> s = Optional.of(1).stream();

Turning an Optional into a Stream is especially useful when composing complex Streampipelines.

--

--