Java 9 Features

In this tutorial, we are going to learn about Java 9 Features.


Oracle was announced to released Java 9 in 2016. In early 2016 the release of Java 9 was rescheduled for March 2017 and changed again and finally available on September 21, 2017.

Java 9 comes set of new rich features. Although there are no new language concept, multi-gigabyte heaps, better native code integration, a self-tuning JVM and diagnostic commands will definitely be interesting to developers.

So It is the right time to learn Java 9. In this tutorial, we are going to have quick and high level look at some of the new features; officially, full list of new features is available here.


The Java Shell (jShell) : Read-Eval-Print Loop (REPL)

Java 9 introduced a new tool called JShell. It stands for Java Shell and also knows as Read Eval Print Loop (REPL). The JShell API and tool will provide a way to interactively evaluate declarations, statements, and expressions of the Java programming language. This tool will provide an easy way to execute and test Java class, interface, enum, object, statement etc.

Read this: How to set up and Installed JDK 9 and JShell or Java Shell

jshell Example

G:\>jshell | Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell> String name = “I am Subhankar” jshell> name

It is very easy to use and develop the application. No need to defined “public class” and “public static void main(String args[])” method, only print the message.

No need to use semicolon for simplifying the statement.

Learn more about JShell and REPL, read JShell and REPL Tutorial with example.

Lots of Improvement in Process API

Java SE 9 is coming with some improvement in Process API. They have added new classes and method to improve the API for controlling and managing the Operating-System processes.

Two new interface in Process API:

  • java.lang.ProcessHandle
  • java.lang.ProcessHandle.Info

Process API example

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

New HTTP/2 Client API

Java 9 comes with new features of HTTP 2 Client API to support HTTP/2 protocol and WebSocket. Legacy of existing HTTP Client API has many issues. It was supported only HTTP/1.1 protocol and did not support HTTP/2 protocols and WebSocket. It supports only in Synchronous(Blocking) mode and lots of performance issues.

New HTTP Client API resolve all these issues and replacing this HttpURLConnection API with new HTTP client. New HTTP 2 Client API under “” package. It supports both HTTP/1.1 and HTTP/2 protocols, also supports Synchronous (Blocking Mode) and Asynchronous Modes. It supports Asynchronous Mode using WebSocket API.

HTTP 2 Client Example

jshell> import* jshell> import static* jshell> import static* jshell> URI uri = new URI("") uri ==> jshell> HttpResponse response = HttpRequest.create(uri).body(noBody()).GET().response() response ==> jshell> System.out.println("Response was " + response.body(asString()))

Introduce new Factory Methods for Immutable Collection Interface and Map

Java SE 9 introduced some new convenient methods to create an Immutable List, Set, Map and Map.Entry objects. In Java 8 and earlier version provides utility method unmodifiableXX of Collections class to create immutable Collection objects.

For example, to create an immutable List then we can use Collections.unmodifiableList() method.

This Collections.unmodifiableList method is more expensive and time taking approach. To overcome those issues, Java SE 9 introduced few utility methods to Collection Interface, List, Set and Map Interface.

List Interface provides “of()” method to create an immutable List and Set.

List example

List immutableList = List.of(); // Empty immutable List List immutableList = List.of("one","two","three"); // Non empty immutable List

Map has two methods “of()” methods and “ofEntries()” to create an immutable Map object.

Map example

Map emptyImmutableMap = Map.of() // Empty immutable Map object jshell> Map nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three") // Non- Empty immutable Map object

Private methods in Interfaces

From Java 8, allow creating an Interface using Default and Static methods. However, we cannot create private methods in Interfaces.

To avoid redundant code and misconception in Java SE 9 support private methods in Interfaces. From Java SE 9 onwards, we can use private and private static methods in Interfaces. Private methods are same like Class private methods.

public interface Car{ private Long carNo(){ // Method implementation goes here. } private static void displayCarDetails(){ // Method implementation goes here. } }

Module System

In Java 9 added a new feature is called Module System. Before Java 9, we have to use set of Monolithic jars to develop the application using java language and in this architecture has lots of drawbacks. In Java 9 removes all these drawbacks and provides a solution using of Module System.

In Java 9 introduce the following modular features:

  • The Modular JDK
  • Modular Source Code
  • Modular Run-Time Images
  • Modular Java Application Packaging

Improvement in Exception Handling

As we know, Java 7 introduced a new concept of Exception Handling using of Try-With-Resources to manage all resources automatically for better resource management.

In Java 9 provides some improvement of Try-With-Resources exception handling methodology and trying to improve the readability.

In Java SE 7

void beforeJava9() throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("subhankar.txt")); try (BufferedReader reader2 = reader1) { System.out.println(reader2.readLine()); } }

In Java SE 9

void afterJava9() throws IOException{ BufferedReader reader1 = new BufferedReader(new FileReader("subhankar.txt")); try (reader1) { System.out.println(reader1.readLine()); } }

Improvement in CompletableFuture API

Java 8 has introduced Class CompletableFuture under the concurrent package to enhance the functionality of the Future interface. There have some bugs raised by the Java community.

In java 9 has comes with some improvement in CompletableFurure API and fixes all the bugs raised in Java 8. It also enhances the utility method by adding a new function of Delay and Timeout.

Diamond Operator for Anonymous Inner Class

Diamond Operator has introduced in Java 7 to avoid redundant code and more readability. However, it has some limitation and it does not support with Anonymous Inner Class.

In Java 9 resolve this problem and allow Diamond Operator for Anonymous Inner Class.

public List getEmployee(String empId){ // Code to get Employee details from Data Store return new List(empObj){ }; }

Here we are using just “List” without specifying the type parameter.

Other Java 9 Features

In this section, I will list out all the other Java 9 features. These features are also important. I will cover all these features in later.

  • Stream API Improvements
  • Optional Class Improvements
  • Reactive Streams
  • Make G1 the Default Garbage Collector
  • Stack-Walking API
  • Filter Incoming Serialization Data
  • Deprecate the Concurrent Mark Sweep (CMS) Garbage Collector
  • SHA-3 Hash Algorithms
  • Compact Strings
  • Store Interned Strings in CDS Archives
  • Indify String Concatenation
  • Unified GC Logging
  • Enhanced Deprecation
  • Platform Logging API and Service
  • Java-Level JVM Compiler Interface
  • Datagram Transport Layer Security (DTLS)
  • Remove GC Combinations Deprecated in JDK 8
  • Unified JVM Logging

That is all about Java 9 features in brief. I will be discussed later all the features in depth.

Read this: Step by step Installation guide on JDK 9 and JShell or Java Shell

Read my previous articles How Internet of Things (IoT) brings revolution in technology

Subhankar Adhikary

I am software professional having more than 4 years of experience in various web technologies mostly in Java. I have strong knowledge in Core Java, Spring, Hibernate, Grails, Web Application, Web Security etc. My hobbies are Developing Software, Learning new technologies, Reading books and Sharing my knowledge through writing articles on PamphletScoop.

Leave a Reply

Your email address will not be published. Required fields are marked *