Open In App

Java 9 Features with Examples

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 Sep 21, 2017.

Features of Java 9

There are a few new features mentioned below:

In this article, we will look into Java 9 features in detail.

1. Improved Javadoc

Java 9 update came with updated Java documentation. We no longer need to use Google to find the right documentation. The new Javadoc came with search rights 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.

Java_Docs

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

Many a time, 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.

Jshell

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 interface 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. Process API Improvements

Process API is used for performing Operating System(OS) level operations for controlling and managing the processes.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.

New interface added in Process API are mentioned below:

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

11. CompletableFuture API Improvements

CompletableFuture API can be used with “java.util.concurrent” package in Java. It helps to work with Asynchronous computations. Some of the features that were added to this API in Java 9 are mentioned below:

  • New Factory Methods
  • Support for Delays
  • Improved Subclassing Support
  • New Methods for Handling Result Transformation
  • Improved Exception Handling

12. Reactive Streams

Reactive Streams API is used for efficiently handling the Asynchronous Data Streams. It uses interfaces and class for implementing the reactive streams. Components of Reactive Streams API in Java are mentioned below:

  1. Publisher: interface represents a provider of a potentially unbounded number of sequenced elements.
  2. Subscriber: interface represents a consumer of a potentially unbounded number of sequenced elements
  3. Subscription: interface represents link between “Publisher” and “Subscriber”.
  4. Processor: interface represents a processing stage which is both “Subscriber” and “Publisher”.

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads