Java 9 in a nutshell

Java 9 in a nutshell

Dawid Kulig
java-9

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:

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:

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:

Methods:

 

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.
  • Header compression – redundant headers are removed therefore its size will be significantly reduced.
  • 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:

Set:

Map:

 

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.

1 Comments

Leave your comment

Bitnami