Features and Improvements of Java 9

Features and Improvements of Java 9 APTRON

The introduction of Java 9 and its features is fairly an achievement in the Java ecosystem. The modular framework that has been produced under the Project Jigsaw would be a part of the Java SE release and majority of the features in this are JShell (REPL TOOL), JVM-level changes and significant API changes in order to enhance the performance as well as the debug ability of the JVM.  

Prior to explaining the Java 9 features in detail, let’s have a look at the previous versions of Java and examine their shortcomings and also how Java 9 helps to overcome those:-

  • Small computing devices could not navigate with the Java Standard Edition platform as well as the JDK.
  • There happened to be no complete security as well as maintenance of the JDK.
  • Application performance showed no improvement at all.  
  • It was quite tough for the Java developers for upholding and building up of the code libraries and also other major applications for Java SE as well as EE platforms.

In this particular blog post I would further categorize the feature of Java 9 as follows:

  1. Process API updates of Java 9
  2. HTTP/2 client in Java 9
  3. Java Shell Scripting in Java 9
  4. Java 9 Multi-release JAR files feature
  5. Java 9 More Concurrency Updates feature
  6. Java 9 Project Jigsaw

What’s new in Java 9?

In this blog post, we have discussed significant Java 9 features that are worth knowing. Let’s have a look on these:

Process API updates of Java 9:

Java’s Process API has been quite aboriginal with additional support to launch the new processes, redirect its output and the error streams. In this particular release the upgrades to the Process API enable the following:

  • Obtaining the PID of the ongoing JVM process and the other processes released by the JVM.
  • Enlist the processes that run in the system to obtain information like PID, resource usage and the name.
  • Managing of the process trees
  • Managing the subprocesses

Let’s have a look at sample illustration of the code that is responsible for printing the current PID and the process information:

1
2
3
4
5
6
7
8
9
10
11
12
public class NewFeatures{
      public static void main(String [] args) {
            ProcessHandle currentProcess = ProcessHandle.current();
            System.out.println("PID:"+ currentProcess.getPid());
            ProcessHandle.Info currentProcessInfo = currentProcess.info();
      System.out.println("Info:" + currentProcessInfo);
}

HTTP/2 client in Java 9:

The Java 9 feature can further introduce changes in the further releases and can also be entirely removed. Earlier the developers usually adopted the third-party libraries, like Jersey, Apache HTTP and so on. Additionally it is found that the Java’s HTTP API necessarily predates the HTTP/1.1 specializations and is synchronous as well as hard to maintain. These limitations further call up for the requirements of adding the new API. The new HTTP client API offers the following:

  • Concise and simple API to deal with majority of HTTP requests
  • Supports the specifications for HTTP/2
  • Better performance and other enhancements.    

Now, let us have a look at the sample code for making an HTTP GET request using the new APIs. Following is the module definition that has been defined in the file module-info.java:

 

1
2
3
module newfeatures{
       requires jdk.incubator.httpclient;
   }

Below mentioned is the code that uses the HTTP Client API and is a part of jdk.incubator.httpclient module:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import jdk.incubator.http.*;
import java.net.URI;
public class Http2Feature{
     public static void main(String[] args) throws Exception{
       HttpClient client = HttpClient.newBuilder().build();
       HttpRequest request = HttpRequest
.newBuilder(new URI(http://httpbin.org/get;))
.GET()
.version(HttpClient.Version.HTTP_1_1)
.build();
HttpResponse.String response = client.send(request,
HttpResponse.BodyHandler.asString());
System.out.println("Status code:" + response.statusCode());</pre>
<pre>System.out.println("Response Body:" + response.body());
                       }
          }
}

Java Shell Scripting in Java 9 (Read-Eval-Print-Loop):

You must have noticed that the languages such as Clojure, Scala, Groovy, Ruby and many others while shipping with a tool and this is usually called as REPL i.e. Read-Eval-Print-Loop. This tool proves to be of great use while using the language features. For Example: By making use of Scala, a simple Hello World program can be written as:  scala>println(“Hello World”);

Following are the advantages of JShell REPL:

  • Experienced developers have the ability to experience and quickly prototype prior to the adoption of their main code base.
  • Java developers can be the proud owner of REPL

The image below showcases how a JShell Command runs:

Java 9

Java 9 Multi-release JAR files feature:

To talk of the present, JAR files comprise of the classes that can be made to run only on the specific Java version for which they were compiled. It is important for the library developers to release the new version of library in order to use the newer features of Java platform on the new version. It is expected that there would be multiple versions of the library soon, which in turn can prove to be a nightmare. In order to overcome this limitation, the Java 9 features of the multi-release JAR files permit the developers to build up JAR files with various versions of the class files for the existing varied Java versions. Following example makes it even clearer:

Following is the illustration of the present JAR files:  

 

jar root 

    - A.class

    - B.class 

    - C.class

 

The multi-release JAR files look like:

:

jar root 

     - A.class 

     - B.class 

     - C.class 

     - META-INF  

      - versions 

             - 9  

                - A.class  

            - 10 

                - B.class

 

In the above illustration, the JAR files support the class files of the two Java versions i.e. 9 and 10.

So, while the earlier JAR is implied on Java 9, the A.class that comes under the versions – 9 folders are picked up for the execution.

The classes under the versions directory are never used on a platform that doesn’t support the multi-release JAR files. Thus it is equally good to run a simple JAR file on Java 8 as the multi-release JAR file.

Java 9 More Concurrency Updates feature:

A newer class i.e. java.util.concurrent.Flow has been introduced that has encapsulated the interfaces, which support the implementation of the publish-subscribe framework. This particular framework gives the developers an ability to build up the components that are supposed to asynchronously consume a live stream of the data. This is done by setting up the publishers that lead to the production of the data as well as the subscribers, which use the data through a subscription that manages them. Following are the four new interfaces:

  • java.util.concurrent.Flow.Publisher
  • java.util.concurrent.Flow.Subscriber
  • java.util.concurrent.Flow.Subscription
  • java.util.concurrent.Flow.Processor (that acts as both Subscriber and Publisher).

Java 9 Project Jigsaw:

The primary objective of the project is to introduce the modularity concept; assistance to create modules in Java 9 and then applying the same to JDK or in other terms modularize the JDK.

Some of the major benefits of the modularity are mentioned below:

Strong encapsulation: The modules can access only those parts, which are available for use. So, unless the package is explicitly exported into the module info file the public classes in a package are not public. 

Clear Dependencies: The modules must declare about the other modules that they would be using through the required clause.

Modules are combined in order to create shorter runtime that can be scaled to comparatively smaller computing devices easily.

Reliable: After the elimination of the run-time errors the applications become more reliable. For Ex: You must have noticed that your applications fail during run-time because of the missing classes, which ultimately results into ClassNotFoundException.

Following are the various JEPs that form to be a part of this project:

JEP 200 – modular JDK: This makes use of the Java Platform module system for modularizing the JDK into ca certain set of modules, which can be combined at build time, compile time or at the runtime.

JEP 201 – modular source code: This modularizes the source code of JDK into modules and upgrades the built tools for compiling the modules.

JEP 220 – modular runtime images: It helps to restructure the JRE and JDK runtime images to accommodate the modules and hence improve the security, performance, and maintainability.

JEP 260 – encapsulate most internal APIs: It permits the majority of internal APIs to be directly accessed or through reflection. It is quite risky to access the internal APIs that are bound to change. To prevent the usage of these, they are compressed into modules and only those internal APIs are made available that are widely used unless the proper API is at its place.

JEP 261 – module system: It is responsible for implementing the module system Java specification by changing JVM, Java programming language and the other standard APIs.  

JEP 282: Jlink, the Java linker: This allows packing of the modules and its dependencies into smaller run times.

Java Training in Noida:

If you are aspiring to build your career in Java Programming then check out the Java Course in Noida at APTRON- the best Java training institute in Noida. In order to gain expertise Java programming then enroll yourself with APTRON’S Java course in Noida. The institute comprises of the industry recommended Java course content in order to prepare the students as per professional standards. 

The Java training in Noida at APTRON comprises more of the practical engagements on real-time java projects for the students to have conceptual understanding. Industry expert corporate professionals with several years of experience conduct the Java training in Noida at APTRON. Exclusive infrastructure with advanced and high-tech lab facilities offers an excellent learning environment.  The duration for the Java training class is flexible i.e. it is conducted in multiple batches in the weekdays and is also conducted at the weekends. Additionally, APTRON also provides fast-track Java training classes.