New Features of Java 9



Java 9 upgrade is almost ready. Early access builds are already out in the wild, with general availability expected in September 2017.

Today we have a very clear picture of what to expect from Java 9 release. If Java 8 was a major release of lambdas, streams and API changes, then Java 9 is all about modules, jshell, and a collection of under the hood and API updates.

Let take a quick look at some of the major features in Java 9.

1. Java 9 Module System

One of the big changes of Java 9 is the Module System. Oracle Corp is going to introduce their Jigsaw Project.

Project Jigsaw’s goal is to make Java modular and break the JRE to inter-operable components.

Modular source code is the first out of 4 steps towards Jigsaw and will not change the actual structure of the JRE and JDK. The purpose of this step is to reorganize the JDK source code into modules, enhance the build system to compile modules, and enforce module boundaries at build time.

Before Java SE 9 versions there is no notion of explicit dependencies between different parts (JAR files) of a system. Every public class can be accessed by any other public class on the classpath, leading to inadvertent usage of classes that weren't meant to be public API. Also, the classpath itself is problematic, how do you know if there are duplicate entries? The module system addresses both issues.

Modular JAR files contain an additional module descriptor. In module descriptor, `requires` statements expresses dependency on other modules. Additionally, `exports` statements control which packages are accessible to other modules, rest all are encapsulated in the module by default.
module MyModule {

  exports com.actigence.finance;

  requires employee;

}

2. Private Methods in Interface

Java 8 allowed Default and Static methods in an Interface. Now Java 9, allows definition of private methods, to avoid redundant code and allow more re-usability.
public interface Employee{

  private Long id(){

    // Method implementation goes here.

  }

  private static void hostedEvents(){

    // Method implementation goes here.

  }

}

3. Improved Process APIs

It has always been a pain to work with OS processes. Java 9 has added some new classes and methods to make it easy to manage and control OS processes.
System.out.println("Current Process Id: = " + ProcessHandle.current().getPid());

4. JShell - Java 9 REPL

REPL stands for Read Evaluate Print Loop. Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. You can launch JShell from the console and directly start typing and executing Java code. JShell makes a good tool to explore APIs and try out language features.
G:\>jshell

|  Welcome to JShell -- Version 9-ea

|  For an introduction type: /help intro


jshell> int x = 10

x ==> 10


jshell> System.out.println("X value = " + x )

X value = 10

These are few of the Java 9  features that will change the way you code in Java.

Stay tuned for more Java 9 updates.

Please leave your feedback in comments section.


0 comments:

Post a Comment