Java 9 Features and Improvements

Last updated on Sep 27,2023 22K Views

Java 9 Features and Improvements

edureka.co

The release of Java 9 and Java 9 features is a milestone for the Java ecosystem. Keeping up with new releases is important for staying up to date with the technology and understanding the need behind what gets introduced will gear you closer to your Java, J2EE & SOA CertificationThe modular framework developed under Project Jigsaw will be part of this Java SE release and major features in this are the JShell (REPL tool), important API changes and JVM-level changes to improve the performance and debuggability of the JVM.

Before we unravel the Java 9 features in detail let us take a peek at previous Java versions and see what the shortcomings were and how Java 9 helped to overcome those anomalies:-  

In this blog post I will categorize Java 9 features in the following manner:

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

What is new in Java 9?

I have picked a few new Java 9 features, which I feel are worth knowing about. Let’s see what are these features:-

Process API updates in Java 9

Java’s Process API has been quite primitive, with support only to launch new processes, redirect the processes’ output, and error streams. In this release, the updates to the Process API enable the following:

Let’s look at a sample code, which prints the current PID as well as the current process information:


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

This Java 9 feature is expected to change in the subsequent releases and may even be removed completely. 

Earlier Developers often resort to using third-party libraries, such as Apache HTTP, Jersey, and so on. In addition to this, Java’s HTTP API predates the HTTP/1.1 specification and is synchronous and hard to maintain. These limitations called for the need to add a new API. The new HTTP client API provides the following:

Let’s see a sample code to make an HTTP GET request using the new APIs. Below is the module definition defined in the file module-info.java:


module newfeatures{
       requires jdk.incubator.httpclient;
   }

The following code uses the HTTP Client API, which is part of jdk.incubator.httpclient module:

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 (Read-Eval-Print-Loop) in Java 9

You must have seen languages, such as Ruby, Scala, Groovy, Clojure, and others shipping with a tool often called REPL (Read-Eval-Print-Loop). This REPL tool is extremely useful in trying out the language features. For example, in Scala, we can write a simple Hello World program as scala>println(“Hello World”);

Some of the advantages of the JShell REPL are as follows:

Let’s run the JShell command, as shown in the following image:

Multi-release JAR files feature in Java 9

Currently, JAR files can contain classes that can only run on the Java version they were compiled for. To leverage the new features of the Java platform on newer versions, the library developers have to release a newer version of their library. Soon, the developers will maintain multiple versions of the library, which can be a nightmare. To overcome this limitation, these Java 9 features of multi-release JAR files allows developers to build JAR files with different versions of class files for different Java versions. The following example makes it more clear. 

Here is an illustration of the current JAR files:

jar root 

    - A.class

    - B.class 

    - C.class

Here is how multi-release JAR files look:

jar root 

     - A.class 

     - B.class 

     - C.class 

     - META-INF  

      - versions 

             - 9  

                - A.class  

            - 10 

                - B.class

In the preceding illustration, the JAR files support class files for two Java versions–9 and 10.

So, when the earlier JAR is executed on Java 9, the A.class under the versions – 9 folders are picked up for execution.

The classes under the versions directory are never used on a platform that doesn’t support multi-release JAR files. So, running the multi-release JAR file on Java 8 is as good as running a simple JAR file.

Preparing for the Java Interview? We have compiled the list of most frequently asked Java Interview Questions and Answers to help you get prepared.

More Concurrency Updates feature in Java 9

In this update, a new class, java.util.concurrent.Flow has been introduced, with nested interfaces supporting the implementation of a publish-subscribe framework. The publish-subscribe framework enables developers to build components that can asynchronously consume a live stream of data by setting up publishers that produce the data and subscribers that consume the data via subscription, which manages them. The four new interfaces are as follows:

Project Jigsaw in Java 9

The main aim of this project is to introduce the concept of modularity; support for creating modules in Java 9 and then apply the same to the JDK; that is, modularize the JDK.

Some of the benefits of modularity are as follows:

There are various JEPs, which are part of this project, as follows:

So, this was all about Java 9 and new Java 9 features.

Now that you have understood features of Java 9, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this “Java 9” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial