Java 9 Features and Improvements

Kavya Tolety
Edureka
Published in
7 min readJul 5, 2021

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 Certification. The 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 were the shortcomings 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, which is 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:

Multi-release JAR files feature in Java 9

As of now, 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, there will be multiple versions of the library being maintained by the developers, 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.

On a platform that doesn’t support multi-release JAR files, the classes under the versions directory are never used. So,if you run the multi-release JAR file on Java 8, it’s as good as running a simple JAR file.

More Concurrency Updates feature in Java 9

In this update, a new class, java.util.concurrent.Flow has been introduced, which has 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.With this, we come to an end to this blog. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on May 22, 2019.

--

--

Kavya Tolety
Edureka
Writer for

Hey! Just a normal girl on the internet who loves to read, research and analyze