Java 9 in a nutshell

Java 9 in a nutshell

It’s been three years since Java 8 was officially released. Thanks to the hard work of Oracle engineers, July 27th, 2017 is the final date when the ninth Java generation becomes available. This major release will come with a rich feature set that Java developers will definitely be excited about. Here are five new features which are an absolute must know about in Java 9. Some say these will change the way how you develop software.

1. 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 require 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:

1

2

3

4

5

6

7

module com.montrosesoftware.utils{

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

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

}

This simple module declaration says to depend on (include) com.google.guava and export one package com.montrosesoftware.utils. All other packages are implicitly hidden.

2. JShell (REPL)

Many modern compiled languages by default provide tools used for statements interpretation. Using these, you can quickly test your code snippets without sacrificing just one minute on creating a new project. Unfortunately Java was not that one. How many times did you create a new Java project i.e Main class alongside with the public static void main method just in order to check something like:

1

2

3

"abc"==newString("abc")

Here, Java 9 comes with JShell, an interactive Read-Eval-Print-Loop utility that lets you run Java statements without wrapping these into classes or methods.

Expressions:

1

2

3

4

5

6

7

8

jshell&gt;6+10

$1==&gt;16

jshell&gt;inta=4

a==&gt;4

jshell&gt;a+5

$3==&gt;9

Methods:

1

2

3

4

5

6

7

jshell&gt;voidprintHelloWorld(){System.out.println("Hello, World!");}

|created method printHelloWorld()

jshell&gt;printHelloWorld()

Hello,World!

3. HTTP/2.0

HTTP/2 is the latest version of the HTTP/1.1 protocol that was released in 1999, which has been aging for nearly 20 years, so web pages are unable to load as fast as it is technologically feasible. Below are the key improvements

Multiplexing (concurrency) – there might be only one connection between server and client, because several requests can be sent within the same TCP connection.

Server push – server has ability to send resources the client has not yet requested.

Binary format – all communication is done in the binary format. However, this format is less readable, it reduces the amount of errors and inefficiencies compared to textual HTTP/1.1.

In Java 9 new HTTP/2 client is coming to the town under the java.net.http package. It can work with both HTTP/1 and HTTP/2 protocols and support Synchronous and Asynchronous modes.

4. Reactive Streams

One of the biggest wins of Java 8 was introduction of the Stream API to perform filter/map/reduce like operations on the collections. Nowadays reactive programming is getting more and more popular, therefore Oracle Corp has given another boost to the Stream API by introducing Reactive Streams. You will find the enhanced API under the java.util.concurrent.Flow package.

The Flow API mixes ideas of both Observer and Iterator patterns. The Iterator is a pull model where Observer has a push responsibility (data from source is pushed to the application). Using the Flow API, publisher pushes at most the same amount of resources that Subscriber (application) has requested.

5. Collection factory methods

One of the most irritating thing about Java was its verbosity when it comes to doing the most basic of things. In order to create unmodifiable list you have to construct the object, store its reference, invoke add() method and wrap it with an unmodifiable interface.
Thankfully, Java 9 comes with several Factory Methods to create immutable collections.

List:

1

2

3

4

List.of();// immutable empty list

List.of("foo","bar");// immutable list with 2 elements

Set:

1

2

3

4

Set.of()// immutable empty set

Set.of("foo","bar");// immutable set with 2 elements

Map:

1

2

3

4

Map.of()// immutable empty map

Map.of("foo","bar")// immutable map with one key-value pair

Summary

The new Java version brings wide array of exciting features. Some of these are key changes and some just a small enhancements. In my article I focused only on five in my opinion the most important facilities. If you are interested in the full feature list, please visit the official Oracle docs at https://docs.oracle.com/javase/9/whatsnew/toc.htm.

Leave your comment

More Articles

A Transparent Trustless Lottery

Advances of the blockchain based technologies gave birth to Ethereum (https://www.ethereum.org/) which opened a whole world of possibilities for “programming money”. At Montrose we decided to have a hackathon to dwell deeper into the possibilities that the new technology offers. We discussed the timeline and the challenges and we have settled on developing two separate …

Dates and times in Java – the unexpected timezone shift

Introduction: Dates and times are an area of software that seem easy until you actually work with them, especially across timezones and when databases and Java are involved. We created DbAssist to make it a little bit easier. The article presents a clean and simple way to solve the well-known issue with JDBC adding improper …