Header Ads

Java 9 Features




Java 9 Features




After nearly 3 years of disagreements and agreements on controversial Project Jigsaw, Java 9—formally, Java Platform Standard Edition version 9 is finally here. Java SE 9 has various key architectural as well as component changes, making SE 9, the major feature release for Java Platform. The following are mentioned some of the key features and enhancements of Java 9:



Module System (Jigsaw Project):


Though most significant feature of Java 9 is Module System, still it is very controversial. It is released as a part of Jigsaw Project by Oracle Corporation to ease the woes of Java SE previous versions. Following features are part of the Jigsaw Project:

         1. Modular JDK
         2. Modular Java Source Code
         3. Modular Run-time Images
         4. Encapsulate Java Internal APIs
         5. Java Platform Module System

As in Java 9 Development Services, JDK, JRE, JARs etc. are divided into smaller modules, so developers can implement only those modules that are demanded to develop a particular application. In addition, Module system provides ease of Testing and Maintainability, support to better Performance, support for strong Encapsulation, support to less coupling between components, support to Single Responsibility Principle (SRP) and restricts access to Internal Non-critical APIs.


JDK 9 Folder Structure:

JDK 9 folder structure, unlike JDK 8 folder structure, doesn’t possess JRE folder. JRE is separated into a different folder named ‘jmods’, that contains a set of Java 9 modules and is available at ${JAVA_HOME}/jmods. ‘jmods’ which has approximately 95 modules collectively known as “JDK Modules”.




Java SE 9 Module:

Java 9 module is a self-explanatory collection of the following main components:

One Module
Module Name
Module Descriptor
Set of packages
Set of types and resources
In Java 9, JDK jars and Java SE Specifications are separated into two sets of modules – All JDK modules start with “jdk”, while all Java SE specifications modules which start with “java”. Java SE 9 has a “java.base” module commonly known as base module and acts as the heart of all Java 9 modules (all JDK modules and User-defined modules).

Developers can create their own modules as shown below:


Here, ‘module’ is used to create a simple module.

Java 9 application with added component Modules can be understood from the below figure:

java9_app




Jshell (Java 9 REPL):

Java 9 features the Read-Eval-Print-Loop (REPL) tool most commonly known as Jshell. Java 9’s REPL feature interactively evaluates declarative statements and expressions. With this, developers can receive feedback for programs before compilation, just by adding few lines of code. It does not require any IDEs or Editors to execute simple Java development services.

It is used to execute and test any Java Constructs like class, interface, enum, object, statements, etc. very efficiently and quickly.




Reactive Streams:

Reactive programming has become very popular in application development with technologies like Scala,Play, Akka, etc. In Java 9, new Reactive Streams API are introduced to achieve similar features as of reactive programming.

Java SE 9 Reactive Streams API is a Publish/ Subscribe Framework used to implement Asynchronous, Scalable and Parallel applications easily. Java SE 9 has introduced the following APIs to develop Reactive Streams in Java-based applications:

java.util.concurrent.Flow
java.util.concurrent.Flow.Publisher
java.util.concurrent.Flow.Subscriber
java.util.concurrent.Flow.Processor
Multi-Resolution Images:
A new multi-resolution image API, defined in java.awt.image package, let a set of images with different resolutions to be wrapped up into a single multi-resolution image. Below mentioned basic operations can be performed on a multi-resolution image:

To retrieve a resolution-specific image variant based on a given DPI metric and set of image transformations, and
To retrieve all of the variants in the image
Apart from these operations, a multi-resolution image will behave alike an ordinary image. The java.awt.Graphics class will retrieve the necessary variant from a multi-resolution image based on the current display DPI metric and any applied transformations.



Private Methods in Interface:

In Java SE 8, developers were allowed to write method implementation in interfaces by using Static and Default methods. But this feature led to code redundancy. To remove the code redundancy, one way was to extract common code in a public method which led to a threat of exposing code to other modules and clients directly.

Java SE 9 introduced a new feature to interfaces to give answer to the raised issues – Private methods in interface. Interfaces can have any of the following members:

Constant Variables
Abstract Methods
Default Methods
Static Methods
Private Methods
Private Static Methods
Example:-




Here, redundant code has been extracted into a common private method so that API Clients cannot see the crucial code.

Process API:

In Process API, Oracle team added a couple of new classes and methods to ease the controlling and managing of OS processes. Two new interfaces added in Process API are:

java.lang.ProcessHandle
java.lang.ProcessHandle.Info
Process API example

ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("Current Process Id: = " + currentProcess.getPid());

Some of the information, developers can now obtain from Process instance which includes:

Whether the process supports normal termination (i.e. any of the “non-forcible” kill signals in Linux)
The process ID (i.e. the “pid”)
A handle to the current process
A handle to the parent process, if one exists
A stream of handles to the direct children of the process
A stream of handles to the descendants (direct children, their children, and so on recursively)
A stream of handles to all processes visible to the current process
Process metadata such as the full command line, arguments, start instant, owning user, and total CPU duration


Try-with Resources:

Java SE 9 has added a few improvements in the Java SE 7’s new exception handling construct: Try-With-Resources, to overcome the flaws in Java SE 7.

Try-With-Resources Example-Java SE 7:



Here, a utility method which creates a BufferedReader object to read the content of a file is created. If the above code snippet is observed, even though there is reader1 referring to BufferedReader object, a duplicate “reader2” BufferedReader object has been created to use it in Try-With-Resources. It is one small bug or issue in Java SE 7 or 8 versions.

In Java SE 9 to overcome the above mentioned issue, if there is a resource already declared outside the Try-With-Resource Statement as final or effectively final, so now there is No need to declare a local variable. Previously created variable can be used within Try-With-Resource Statement without any issues as shown below:



Diamond Operator Extension:

Java SE 9 has provided extension to the Java SE 7 feature with Diamond Operators, in order to remove the limitations and issues with Anonymous Inner Classes. Following code snippet shows the limitation of diamond operator with Anonymous Inner classes in Java SE 7:







The above code throws compilation error in Java SE 7 & 8 while it runs without compilation error in Java SE 9 due to the Diamond Operator Extension introduced.



Unified JVM Logging:

Java 9 came up with a unified logging architecture (JEP 158) that pipes a lot of messages that the JVM generates through the same mechanism, which can be configured with the -Xlog option especially for complicated Java Software Development. This gives uniform access to log messages from different subsystems such as class loading, threading, garbage collector, module system, or interaction with the underlying operating system.

By simply executing java -Xlog, appending -version gives the below output:

$ java -Xlog -version

# truncated a few messages

> [0.002s][info][os ] HotSpot is running with glibc 2.23, NPTL 2.23

# truncated a few messages

It shows how long the JVM has been running (2 ms), the message’s log level (info), its tags (only os), and the actual message.



SafeVarargs Scope Extension:

Java 7 introduced the SafeVarargs annotation type for asserting that the bodies of annotated final or static methods, or constructors don’t perform potentially unsafe operations on their varargs (variable number of arguments) parameters. Java 9 expands this capability to also include private methods.

SafeVarargs must be used with methods that can’t be overridden because an overriding method could violate its superclass method’s @SafeVarargs annotation by performing an unsafe operation. Static, final, and private methods, and constructors can’t be overridden, so they are used with SafeVarargs.





HTTP 2 client:

New HTTP 2 Client API to support HTTP/2 protocol and WebSocket features has been introduced in Java SE 9. Existing HTTP Client API has numerous issues like supports HTTP/1.1 protocol and doesn’t support HTTP/2 protocol and WebSocket, works only in “Blocking mode” and lot of performance issues. This HttpURLConnection API is being replaced with new HTTP client.

This API is being introduced under “java.net.http” package. It supports both HTTP/1.1 and HTTP/2 protocols and both Synchronous (Blocking Mode) and Asynchronous Modes. The Asynchronous Mode is supported using WebSocket API.

HTTP 2 Client Example:




 Today's inspirational quotes: 

"Technology will never replace great teachers but
technology in the hands of great teachers is
transformational"



Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)

Happy Reading.Happy Learning!!! See you in next post :)

No comments

Note: Only a member of this blog may post a comment.