Java 9 Features with Examples

Filed Under: Java

Java 9 is a major release and it has brought us a lot of features for developers. In this article, we will look into Java 9 features in detail.

Java 10 has been released, for a complete overview of Java 10 release, go through Java 10 Features.

Java 9 Features

java 9 features
Some of the important java 9 features are;

  1. Java 9 REPL (JShell)
  2. Factory Methods for Immutable List, Set, Map and Map.Entry
  3. Private methods in Interfaces
  4. Java 9 Module System
  5. Process API Improvements
  6. Try With Resources Improvement
  7. CompletableFuture API Improvements
  8. Reactive Streams
  9. Diamond Operator for Anonymous Inner Class
  10. Optional Class Improvements
  11. Stream API Improvements
  12. Enhanced @Deprecated annotation
  13. HTTP 2 Client
  14. Multi-Resolution Image API
  15. Miscellaneous Java 9 Features

Oracle Corporation is going to release Java SE 9 around the end of March 2017. In this post, I’m going to discuss “Java 9 Features” briefly with some examples.

Java 9 REPL (JShell)

Oracle Corp has introduced a new tool called “jshell”. It stands for Java Shell and also known as REPL (Read Evaluate Print Loop). It is used to execute and test any Java Constructs like class, interface, enum, object, statements etc. very easily.

We can download JDK 9 EA (Early Access) software from

|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> int a = 10
a ==> 10

jshell> System.out.println("a value = " + a )
a value = 10

If you want to know more about REPL tool, Please go through Java 9 REPL Basics (Part-1) and Java 9 REPL Features (Part-2).

Factory Methods for Immutable List, Set, Map and Map.Entry

Oracle Corp has introduced some convenient factory methods to create Immutable List, Set, Map and Map.Entry objects. These utility methods are used to create empty or non-empty Collection objects.

In Java SE 8 and earlier versions, We can use Collections class utility methods like unmodifiableXXX to create Immutable Collection objects. For instance, if we want to create an Immutable List, then we can use Collections.unmodifiableList method.

However, these Collections.unmodifiableXXX methods are a tedious and verbose approach. To overcome those shortcomings, Oracle Corp has added a couple of utility methods to List, Set and Map interfaces.

List and Set interfaces have “of()” methods to create an empty or no-empty Immutable List or Set objects as shown below:

Empty List Example

List immutableList = List.of();

Non-Empty List Example

List immutableList = List.of("one","two","three");

The Map has two sets of methods: of() methods and ofEntries() methods to create an Immutable Map object and an Immutable Map.Entry object respectively.

Empty Map Example

jshell> Map emptyImmutableMap = Map.of()
emptyImmutableMap ==> {}

Non-Empty Map Example

jshell> Map nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three")
nonemptyImmutableMap ==> {2=two, 3=three, 1=one}

If you want to read more about these utility methods, please go through the following links:

Private methods in Interfaces

In Java 8, we can provide method implementation in Interfaces using Default and Static methods. However we cannot create private methods in Interfaces.

To avoid redundant code and more re-usability, Oracle Corp is going to introduce private methods in Java SE 9 Interfaces. From Java SE 9 onwards, we can write private and private static methods too in an interface using a ‘private’ keyword.

These private methods are like other class private methods only, there is no difference between them.

public interface Card{

  private Long createCardID(){
    // Method implementation goes here.

  private static void displayCardDetails(){
    // Method implementation goes here.


If you want to read more about this new feature, please go through this link: Java 9 Private methods in Interface.

Java 9 Module System

One of the big changes or java 9 feature is the Module System. Oracle Corp is going to introduce the following features as part of Jigsaw Project.

  • Modular JDK
  • Modular Java Source Code
  • Modular Run-time Images
  • Encapsulate Java Internal APIs
  • Java Platform Module System

Before Java SE 9 versions, we are using Monolithic Jars to develop Java-Based applications. This architecture has a lot of limitations and drawbacks. To avoid all these shortcomings, Java SE 9 is coming with the Module System.

JDK 9 is coming with 92 modules (may change in final release). We can use JDK Modules and also we can create our own modules as shown below:

Simple Module Example

module { }

Here we are using ‘module’ to create a simple module. Each module has a name, related code, and other resources.

To read more details about this new architecture and hands-on experience, please go through my original tutorials here:

Process API Improvements

Java SE 9 is coming with some improvements in Process API. They have added couple new classes and methods to ease the controlling and managing of OS processes.

Two new interfcase 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());

Try With Resources Improvement

We know, Java SE 7 has introduced a new exception handling construct: Try-With-Resources to manage resources automatically. The main goal of this new statement is “Automatic Better Resource Management”.

Java SE 9 is going to provide some improvements to this statement to avoid some more verbosity and improve some Readability.

Java SE 7 example

void testARM_Before_Java9() throws IOException{
 BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));
 try (BufferedReader reader2 = reader1) {

Java 9 example

void testARM_Java9() throws IOException{
 BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));
 try (reader1) {

To read more about this new feature, please go through my original tutorial at: Java 9 Try-With-Resources Improvements

CompletableFuture API Improvements

In Java SE 9, Oracle Corp is going to improve CompletableFuture API to solve some problems raised in Java SE 8. They are going add to support some delays and timeouts, some utility methods and better sub-classing.

Executor exe = CompletableFuture.delayedExecutor(50L, TimeUnit.SECONDS);

Here delayedExecutor() is a static utility method used to return a new Executor that submits a task to the default executor after the given delay.

Reactive Streams

Nowadays, Reactive Programming has become very popular in developing applications to get some beautiful benefits. Scala, Play, Akka, etc. Frameworks have already integrated Reactive Streams and getting many benefits. Oracle Corps is also introducing new Reactive Streams API in Java SE 9.

Java SE 9 Reactive Streams API is a Publish/Subscribe Framework to implement Asynchronous, Scalable and Parallel applications very easily using Java language.

Java SE 9 has introduced the following API to develop Reactive Streams in Java-based applications.

  • java.util.concurrent.Flow
  • java.util.concurrent.Flow.Publisher
  • java.util.concurrent.Flow.Subscriber
  • java.util.concurrent.Flow.Processor

Read more at Java 9 Reactive Streams.

Diamond Operator for Anonymous Inner Class

We know, Java SE 7 has introduced one new feature: Diamond Operator to avoid redundant code and verbosity, to improve readability. However, in Java SE 8, Oracle Corp (Java Library Developer) has found that some limitations in the use of Diamond operator with Anonymous Inner Class. They have fixed those issues and going to release them as part of Java 9.

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

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

Optional Class Improvements

In Java SE 9, Oracle Corp has added some useful new methods to java.util.Optional class. Here I’m going to discuss about one of those methods with some simple example: stream method

If a value present in the given Optional object, this stream() method returns a sequential Stream with that value. Otherwise, it returns an empty Stream.

They have added “stream()” method to work on Optional objects lazily as shown below:

Stream<Optional> emp = getEmployee(id)
Stream empStream = emp.flatMap(Optional::stream)

Here method is used to convert a Stream of Optional of Employee object into a Stream of Employee so that we can work on this result lazily in the result code.

To understand more about this feature with more examples and to read more new methods added to Optional class, please go through my original tutorial at: Java SE 9: Optional Class Improvements

Stream API Improvements

In Java SE 9, Oracle Corp has added four useful new methods to java.util.Stream interface. As Stream is an interface, all those new implemented methods are default methods. Two of them are very important: dropWhile and takeWhile methods

If you are familiar with Scala Language or any Functions programming language, you will definitely know about these methods. These are very useful methods in writing some functional style code. Let us discuss the takeWhile utility method here.

This takeWhile() takes a predicate as an argument and returns a Stream of the subset of the given Stream values until that Predicate returns false for the first time. If the first value does NOT satisfy that Predicate, it just returns an empty Stream.

jshell> Stream.of(1,2,3,4,5,6,7,8,9,10).takeWhile(i -> i < 5 )

To read more about takeWhile and dropWhile methods and other new methods, please go through my original tutorial at: Java SE 9: Stream API Improvements

Enhanced @Deprecated annotation

In Java SE 8 and earlier versions, @Deprecated annotation is just a Marker interface without any methods. It is used to mark a Java API that is a class, field, method, interface, constructor, enum etc.

In Java SE 9, Oracle Corp has enhanced @Deprecated annotation to provide more information about deprecated API and also provide a Tool to analyze an application’s static usage of deprecated APIs. They have add two methods to this Deprecated interface: forRemoval and since to serve this information.

HTTP 2 Client

In Java SE 9, Oracle Corp is going to release New HTTP 2 Client API to support HTTP/2 protocol and WebSocket features. As existing or Legacy HTTP Client API has numerous issues (like supports HTTP/1.1 protocol and does not support HTTP/2 protocol and WebSocket, works only in Blocking mode and lot of performance issues.), they are replacing this HttpURLConnection API with new HTTP client.

They are going to introduce a new HTTP 2 Client API under the “” package. It supports both HTTP/1.1 and HTTP/2 protocols. It supports both Synchronous (Blocking Mode) and Asynchronous Modes. It supports Asynchronous Mode using the WebSocket API.

We can see this new API at

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()))

Please go through my original tutorial at: Java SE 9: HTTP 2 Client to understand HTTP/2 protocol & WebSocket, Benefits of new API and Drawbacks of OLD API with some useful examples.

Multi-Resolution Image API

In Java SE 9, Oracle Corp is going to introduce a new Multi-Resolution Image API. Important interface in this API is MultiResolutionImage . It is available in java.awt.image package.

MultiResolutionImage encapsulates a set of images with different Height and Widths (that is different resolutions) and allows us to query them with our requirements.

Miscellaneous Java 9 Features

In this section, I will just list out some miscellaneous Java SE 9 New Features. I’m NOT saying these are less important features. They are also important and useful to understand them very well with some useful examples.

As of now, I did not get enough information about these features. That’s why I am going to list them here for a brief understanding. I will pick up these features one by one and add to the above section with a brief discussion and example. And finally write a separate tutorial later.

  • GC (Garbage Collector) Improvements
  • Stack-Walking API
  • Filter Incoming Serialization Data
  • Deprecate the Applet API
  • Indify String Concatenation
  • Enhanced Method Handles
  • Java Platform Logging API and Service
  • Compact Strings
  • Parser API for Nashorn
  • Javadoc Search
  • HTML5 Javadoc

I will pickup these java 9 features one by one and update them with enough description and examples.

That’s all about Java 9 features in brief with examples. [no_toc]


  1. Janakirami reddi says:

    nice article

  2. Ramya says:


    There is no seperate hyperlink for the below articles mentioned above. It will be better and useful for us if you kindly enable the hyperlink for them.

    1. Java SE 9: Process API Improvements.
    2. Java SE 9: CompletableFuture API Improvements
    3. Java SE 9: Diamond Operator improvements for Anonymous Inner Class
    4. Java SE 9: Enhanced @Deprecated annotation

  3. Binh Thanh Nguyen says:

    Thanks, nice summary.

  4. Nitin Jagdale says:

    The comparison of List.of(…) or Collections.unmodifiableList() is wrong, please check below thread:

  5. Deepanshu says:

    Thanks. Its useful

  6. Rohit R Chidrewar says:

    Excellent post. Thank you.

  7. Nitin says:

    As I understand List immutableList = List.of(); would create an empty immutable list.
    But, what is the point, why would it be used for?

  8. Dolly says:

    Its really nice content. but some of the links which you have specified for detail article are not redirecting to specified page like Enhanced @Deprecated annotation .Can you please check. and update

    1. Pankaj says:

      Can you please post the link that is not working? I rechecked and everything seems to be working fine.

      1. Shinu says:

        Java 9 Module System Basics
        Java 9 Module System Examples

        These links are not included

        1. Pankaj says:

          Thanks for noticing, those were in the draft when this article went live. Later I forgot to add them here, updated the post with links to java 9 module system articles. Appreciate it man.

  9. SreeCharan Shroff says: instead of currentProcess.getPid()

  10. Lace Frontal says:

    Awesome information. Keep it up…

    Thank you…

  11. Vijendra says:

    Why java required so much of ram to run programe???.
    Remove garbage collection as in ios there is no concept of gc.
    Try to manage system resources there is no Management of memory at all. I cannot run java on 2 GB ram of windows

    1. aditya says:

      no problem at all with RAM you can run java on 1 GB

  12. Umesh says:

    Kindly provide the links to the posts you have mentioned in this article e.g. Reactive system – original tutorials at ??

    1. Pankaj says:

      It’s a work in progress for some editing, I will add the link when it’s published.

  13. Vinayak A says:

    Hi Lokesh,

    Thanks for sharing such a nice post.

  14. vinayak M pilllai says:


  15. vinayak M pilllai says:

    very nice………….

  16. Manpreet says:

    Quite Nice Explaination !!!!!!!!!!!!

  17. Mike says:

    These types of features should not be part of Java, because they should be part of third party libraries. This is just pure bloat, and just contributes to making Java more and more convoluted.

    1. Surya says:

      Truth. But I think Java has nothing new to do other than to make such improvements. Core & drastic changes, can be very drastic and may not be well received by all.

      1. Surya says:

        I meant “bad” instead of the second “drastic”

  18. Rapel says:

    Thank you. Very useful article for me. (I love the links to detailed explanations.)

  19. Pankaj Shet says:

    Hi Sir,

    I wish to understand, with Java 9 comming into market, How does it compete with Scala?
    Where is Java 9 good and where is Scala good, Frameworks like Play, Akka, Spark can be worked upon both with Java as well as Scala,
    How does Groovy stand in the competition?(Creator of Groovy, said, if he was knowing that scala is going to be out, he would’nt have created Groovy language itself.)
    Also, Whatever we do in Scala, is possible in JavaScript(Node.JS) as well,
    When to use what?
    What’s the future of each of them?

  20. Nate says:

    Thanks for the awesome write up! Your description of takeWhile() makes it sound identical to filter() though. I had to read up further to see that it stops after the first non-match.

    1. Rambabu says:

      Thanks, good catch. Yes, I got it. Updated.

  21. Sridhar Mohan says:

    Great going !

  22. Sridhar Mohan says:

    It’s really awesome. Before the release , it’s awesome information to share. Great going !!!

    1. darkcg says:

      Learning is a big word. Those features are much more complex than what exposed here. You’re just having a glimpse on new features, but “learning” is an overstatement here.

      1. Rambabu says:

        Hi Thanks for reading my posts. You can go through those feature specific posts to learn them in detail.


  23. vickey says:

    good tutorial. thank you sir

  24. Thierry Lafaye says:

    Very nice recompilation of Java9 series, great to have to higher level preview of what Java9 is going to be like. Keep up the good work!

  25. Giri says:

    Fabulous Java 9 tutorials. Im learning it before release. grt Dude. nice work

  26. Ashraful Rajon says:


Leave a Reply

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

Generic selectors
Exact matches only
Search in title
Search in content