Java 9 features with examples

Java is a general purpose, high-level programming language developed by Sun Microsystems. It is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. Java was meant to follow the “Write Once Run Anywhere” (WORA) principle, i.e., Java is meant to be platform independent.


To learn more about the Java programming language in details, click here.


Like any other software, Java also comes with many different versions as it develops and gets improved, with new features getting added in every major upgrade.
Java 9 was a major upgrade from Java 8 that has brought us a lot of features for developers. Java 9 was released on July 27, 2019. In this article, we will look into Java 9 features in detail:

1. Improved Javadoc

Java 9 update came with an updated Java documentation. We no longer need to use Google to find the right documentation. The new Javadoc came with search right in the API documentation itself. Moreover, the Javadoc output was HTML5 compliant. Every Javadoc page includes information on which JDK module the class or interface comes from.

2. Factory methods for collections(like List, Map, Set and Map.Entry):

Many a times, you want to create a collection (e.g., a List or Set) in your Java program and fill it with some elements. That leads to repetitive coding where you instantiate the collection, followed by several ‘add’ calls. With Java 9, several so-called collection factory methods have been added.
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");

Map has two set 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}

3. JShell: the interactive Java REPL

Oracle Corp. has introduced a new tool called “jshell”. It stands for Java Shell and also known as REPL (Read Evaluate Print Loop). Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. It is used to execute and test any Java Constructs like class, interface, enum, object, statements etc. very easily. You can launch jshell from the console and directly start typing and executing Java code. The immediate feedback of jshell makes it a great tool to explore APIs and try out language features.

4. 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. It allows you to create declarative pipelines of transformations on collections. There are four new methods added to the Stream interface: dropWhile, takeWhile, ofNullable. The iterate method gets a new overload, allowing you to provide a Predicate on when to stop iterating.

5. 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. introduced private methods in Java SE 9 Interfaces. From Java SE 9 on-wards, we can write private and private static methods too in an interface using ‘private’ keyword.

public interface Card{

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

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

}

6. Multi-Resolution Image API:

In Java SE 9, Oracle Corp. introduced 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 and allows us to query them with our requirements.

7. The Java(9) Platform module system:

One of the big changes or java 9 feature is the Module System. Oracle Corp. introduced 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 lot of limitations and drawbacks. To avoid all these shortcomings, Java SE 9 comes with the Module System.

8. Improvements in Process API:

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

9. HTTP/2 Client

A new way of performing HTTP calls arrives with Java 9. 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 new HTTP 2 Client API under “java.net.http” 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 WebSocket API.

HttpClient client = HttpClient.newHttpClient();

HttpRequest req =
   HttpRequest.newBuilder(URI.create("http://www.google.com"))
              .header("User-Agent", "Java")
              .GET()
              .build();


HttpResponse resp = client.send(req, HttpResponse.BodyHandler.asString());

10. Miscellaneous Java 9 Features:

  • 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

___________________________________________________________________________________________________________________
P.S.: Java SE 9 has reached end of support. Users of Java SE 9 should switch to Java SE 10 or later.


Useful links to learn more about Java:
1. Wikipedia
2. Oracle Documentation



My Personal Notes arrow_drop_up

An ordinary kid with a passion for coding

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.