Java 9 Features with Examples

Spring java
4 min readFeb 28


Java 9 was introduced on September 21 2017. It is having lots of features and new enhancements which are going to improve our coding style. We are going to list some of the essential features of Java 9:
• JShell
• JPMS(Java Platform Module System)
• JLINK(Java Linker)
• HTTP/2 Client
• Process API Updates
• Private methods inside interface
• Try with Resources Enhancements
• Factory methods to create Immutable Collections
• Stream API Enhancements
• Diamond Operator Enhancement:
• SafeVarags Annotation
• G1 GC and etc.


JShell stands for Java Shell and is also known as REPL(Read Evaluate Print Loop). Many languages already have this feature from Java 9 onwards Java has also this feature. It is a tool used for testing java code. For this JShell go to the command prompt, type java code, and execute it.


It stands for Java Platform Module System. This is a major change and Java 9 feature is the Module System. The following feature as part of the Jigsaw Project:
• Modular JDK
• Modular Java Source Code
• Modular Run-time Images
• Encapsulate Java Internal API
• Java Platform Module System
Before Java 9, we are using Monolithic Jars to develop Java-based applications. This architecture is having a lot of limitations and drawbacks. To avoid all these problems, Java 9 comes with the JPMS feature.

JLINK(Java Linker)

JLINK is a beautiful feature provided by Java 9. Imagine we can run a 2KB .class file in our system for this we need JRE. But that default JRE size is large. This is the problem with executing a small size of .class file needs a large size of JRE.So Java 9 provide us with a solution with this JLINK. We can use to create our own customised JRE with the help of the JLINK tool. We can create our own JRE with the required class only.

HTTP/2 Client

Java applications can HTTP Requests and HTTP Responses to the web server using HttpURLConnection.This HttpURLConnection is introduced in 1997. This is having some limitations like
• It is difficult to use
• It is used at a time only one request
• It is use text-based data only
• It is using blocking mode
Java 9 to overcome these limitations with HTTP/2 client feature. HTTP/2 client supports:
• It is easy to use
• It is used for multiple requests
• It is use text-based and binary data
• It is using blocking mode and unblocking mode

Process API Updates

Until Java 8 communicates Java Applications with the underline processor is very difficult. With the help of Java 9 communicating Java Applications with the underline processor is very easy because of this feature. From Java 9 onwards Java is programmer-friendly as well as processor-friendly also because of this feature.


To get the process id of the current JVM with Java Code with Java 9:

public class Test{
public static void main(String[] args)throws Exception{
long pid=ProcessHandle.current().pid();
System.out.println("Current JVM Process ID:"+pid);

To get the process id of the current JVM with Java Code until Java 8 we write lots of line codes but with the use of Java 9 write a single line of code for it.

Private methods inside interface

In Java 9 we can define private methods inside the interface. The purpose of this feature without affect the implementation class and reusability of the code.


interface interF{
default void m1(){
default void m2(){
private void m3(){
common code;

Try with Resources Enhancements

This try-with-resources feature was introduced in Java 7. This feature helps to close resources automatically after being used. Until Java 8 you want to use whatever resources in the try block should be created as local reference variables of that resources. Still, from Java 9 we can use references of the resource in a try block that all are already created.

Factory methods to create Immutable Collections

Until Java 8, we can use the Collections class that provides utility methods like unmodifiableXXX to create Immutable Collection objects. If we want to create an Immutable List, then we can use these Collections.unmodifiableList method. In Java 9 Factory methods are special static methods used to create unmodifiable instances of collections. We can use these methods to create the List, Set and Map. The utility method is “of” to use create an immutable instance of Collection.


To create an Immutable List in Java 8:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class MyClass {
public static void main(String args[]) {
List<String> list=new ArrayList<String>();
List<String> imList= Collections.unmodifiableList(list);
System.out.println("Unmodifiable List " + imList);


To create an Immutable List in Java 9:

import java.util.List;
public class MyClass {
public static void main(String args[]) {
List<String> imList= List.of("A","B","C");
System.out.println("Unmodifiable List " + imList);

Stream API Enhancements

In Java 9 Stream Interface added four new methods takeWhile, dropWhile, ofNullable and iterate get new overload.

Diamond Operator Enhancement

Until Java 8 <> operator we can use it only in normal class but from Java 9 onwards we can use the <> operator in the anonymous inner class also. This is an enhancement in <> operator in Java 9.

SafeVarags Annotation

To suppress compiler warnings whenever we are using generic with a varargs method. Until Java 8 SafeVarags annotation is used in static methods, final methods and constructors but from Java 9 onwards it is used in private methods also.


This means Garbage First Garbage Collector. Until Java 8 Parallel GC is the default garbage collector from Java 9 G1 GC is the default garbage collector.


In this, we learnt about Java 9 is introduced and some of its features overview



Spring java

Spring Java tutorials provides well contents to learn java, spring framework and also the architecture of spring technology for beginners and professionals.