Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Java 9 Features and Improvements

Last updated on Sep 27,2023 22.2K Views

9 / 11 Blog from Introduction to Java

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:-  

  • The Java Standard Edition platform and the JDK were not navigable for small computing devices
  • There was no overall security and maintenance of JDK
  • There was no overall improvement in application performance
  • It was difficult for Java developers to build and uphold the code libraries and larger applications, for both the Java SE and EE Platforms

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:

  • Getting the PID of the current JVM process and any other processes spawned by the JVM
  • Enumerate the processes running in the system to get information such as PID, name, and resource usage
  • Managing process trees
  • Managing subprocesses

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:

  • A simple and concise API to deal with most HTTP requests
  • Support for HTTP/2 specification
  • Better performance
  • Better security
  • A few more enhancements

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:

  • Experienced developers can quickly prototype and experiment before adopting it in their main code base
  • Java developers can now boast of a REPL

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

JShell Hello World Example - Java 9 - Edureka

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:

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

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:

  • Strong encapsulation: The modules can access only those parts of the module that have been made available for use. So, the public classes in a package are not public unless the package is explicitly exported in the module info file. 
  • Clear Dependencies: Modules must declare which other modules they would be using via the requires clause.
  • Combining modules to create a smaller runtime, which can be easily scaled to smaller computing devices.
  • Reliable: Applications are more reliable by eliminating run-time errors. Example:- you must have experienced your application failing during run-time due to missing classes, resulting in ClassNotFoundException.

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

  • JEP 200 – modular JDK: This applies the Java platform module system to modularize the JDK  into a set of modules that can be combined at compile time, build time, or runtime.
  • JEP 201 – modular source code: This modularizes the JDK source code into modules and enhances the build tools to compile the modules.
  • JEP 220 – modular runtime images: This restructures the JDK and JRE runtime images to accommodate modules and to improve performance, security, and maintainability.
  • JEP 260 – encapsulate most internal APIs: This allows a lot of internal APIs to be accessed directly or via reflection. Accessing internal APIs that are bound to change is quite risky. To prevent its use, they are being encapsulated into modules and only those internal APIs that are widely used are being made available until a proper API is in its place.
  • JEP 261 – module system: This implements the module system Java specification by changing the Java programming language, JVM, and other standard APIs 
  • JEP 282: jlink, the Java linker: This allows packaging modules and their dependencies into smaller run times. 

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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Java 9 Features and Improvements

edureka.co