Java 9, all the news


Java 9, modular programming and JShell

Waiting for the release of the first stable version of Java 9 scheduled for September 21st we are going to present the new features introduced by the platform.

Modular programming

Among the various and interesting changes, we highlight the introduction of modular programming through the Java Platform Module System (JPMS) , the result of the “Jigsaw” project, and the JShell for the testing and rapid exploration of language functionality.

Modularity provides a mechanism for declaring the dependency of modules that can be identified both in compilation time and at runtime, so that the system is able to move within dependencies to determine the modules an application needs.

With the modularity in Java 9 we have a strong encapsulation, the packages within a module are accessible to other modules only if the module explicitly exports them. It also introduces greater scalability.

Previously the Java platform consisted of a monolithic block and a large number of packages difficult to maintain and update, the system is now modularized into 95 modules and it is possible to create customizable runtimes by choosing the modules you need.

A module can be seen as a high-level aggregation of related Java packages and resources of a different nature such as images, XML files, etc. Each module has a unique name, supports reflection, can declare dependencies on other modules, must explicitly declare the packages it intends to make available to other modules, the services it offers, and finally the services used.

The application structure of a module is very similar to a normal Java library project (JAR), the fundamental aspect is the presence of a configuration file, called, of which we report a simple example:

  module it.html.welcome.java9 {
   requires java.base;

REPL and JShell

Java 9 introduces its REPL (read-evaluate-print loop) through the JShell . JShell offers a command-line environment that allows you to easily explore and experiment with aspects of the language such as instructions, classes, methods and much more.

For example, instead of writing a test class to test the functioning of a block of code that implements an algorithm, we can directly type and execute our code by inserting it into the shell.

Suppose we want to test the operation of the System.out.println () statement , we would normally implement a test class of this type:

  class MyClass {
   public static void main (String [] args) {
    System.out.println ("Hello Java 9!");

The JShell allows to avoid the realization of the class by typing and executing the instruction directly:

  jshell> System.out.println ("Hello Java 9!")
  Hello Java!

Java 9, the new APIs

Even the language APIs have undergone important changes with Java 9, let’s analyze the most interesting ones.

Within the interfaces it is now possible to declare private methods:

  interface MyInterface {
   private void myPrivateMethod () {..}

The Try-With-Resources was then improved allowing operations such as the following:


  void dbMethod (Connection conn) {
    try (conn) {

Previously executable only through:

   void dbMethod (Connection conn) {
    try (Connection c = conn) {

The APIs for the interaction with the operating system processes have been enhanced, for example the execution of the lsLinux command and its output as input of the command grepto filter files txtwould be easily realized as:

  ProcessBuilder lsCmd = new ProcessBuilder (). Command ("ls"). Directory (Paths.get ("/"). ToFile ());
  ProcessBuilder grepTxtCmd = new ProcessBuilder (). Command ("grep", "txt"). RedirectOutput (Redirect.INHERIT);
  List <Process> lsPlusGrep = ProcessBuilder.startPipeline (Arrays.asList (lsCmd, grepTxtCmd));

In the multimedia field, the class has been introduced MultiResolutionImagethat encapsulates a set of images with different resolutions, allowing selection queries based on image height and width.

With Java 9, Reactive Programming was introduced through the Reactive Streams API . These APIs essentially create a framework of type Publish / Subscribe for the implementation of asynchronous, scalable and parallel applications in a very simple way using the Java language.

Other improvements are recorded on the Stream API side. The developers have added new methods to the interface java.util.Stream. Particularly interesting are the methods dropWhile() and takeWhile()purposes of performing functional-style code.

For example, it takeWhile()takes a predicate input and returns a sub-stream of the values ​​of the main stream, stopping when the predicate returns the value falsefor the first time. If this does not happen, an empty stream is returned.

Example from JShell:

jshell> Stream.of(1,2,3,4,5).takeWhile(i -> i < 3 ).forEach(System.out::println);

At the collections level, Java 9 introduces new useful methods for creating immutable lists, sets and maps.

Example from JShell:

jshell> Map immutableMap = Map.of(1, "Element 1", 2, "Element 2", 3, "Element 3")
immutableMap ==> {2=Element 2, 3=Element 3, 1=Element 1}

The improvement of the API does not stop there but also extends to the network protocols and security. Java 9 introduces support for HTTP / 2 clients within package supporting both synchronous ( Blocking Mode ) and asynchronous operation with the use of WebSocket APIs .

In computer networks, Datagrams provide a non-connection-oriented information exchange mechanism. Java 9 adds support for the Datagram Transport Layer Security ( DTLS) protocol that provides secure communication through Datagrams.

X.509 security certificates are used in public key cryptography, Java 9 improves their use by allowing to check whether an X.509 certificate is still valid (OCSP Stapling for TLS) .

Finally, with TLS Application-Layer Protocol Negotiation Extension we have a security enhancement in package to allow applications to choose protocols from a list to communicate securely.



Please enter your comment!
Please enter your name here