Java 9 Features

Java 9 comes with a rich feature set. Although there are no new language concepts in Java 9 Features, new APIs and diagnostic commands will definitely be interesting to developers.

A series of Java Enhancement Proposals (JEPs) has been published on OpenJDK concerning the next major update (Java 9 with the release schedule.

Some of the important Java 9 features are;

  1. Java Platform Module System – Jigsaw Project
  2. A New HTTP Client(HTTP/2 Client)
  3. Process API Improvements
  4. JShell: the interactive Java REPL(Read-Eval-Print Loop)
  5. Private methods in Interfaces
  6. Factory Methods for Collections
  7. Improved Javadoc – Javadoc Search | HTML5 Support
  8. Enhanced @Deprecated Annotation

Let’s discuss about “Java 9 Features” briefly with some examples.

  1. Java Platform Module System – Jigsaw Project

    It is all new feature in Java 9. When codebases grow larger, the odds of creating complicated, tangled “spaghetti code” increase exponentially.

    There are two fundamental problems:

    • It is hard to truly encapsulate code, and there is no notion of explicit dependencies between different parts (JAR files) of a system. Every public class can be accessed by any other public class on the classpath, leading to inadvertent usage of classes that weren’t meant to be public API.
    • The classpath itself is problematic: How do you know whether all the required JARs are there, or if there are duplicate entries?
    Java Platform Module System
    Illustration of Module System – Java 9

    The module system addresses both issues.

    Modular JAR files contain an additional module descriptor. Which contains two attributes:

    1. requires: Dependencies on other modules are expressed through requires statements.
    2. exports: Which packages are accessible to other modules expressed through exports statements.

    Here’s an example of a module descriptor, which defined in

    module post { exports; requires category; }

    The Java platform itself has been modularized using its own module system as well. By encapsulating JDK internal classes, the platform is more secure and evolving it becomes much easier.

    One of the main motivations here is to provide modular JVM, which can run on devices with a lot less available memory. The JVM could run with only those modules and APIs which are required by the application. Check out link for a description of what these modules are.

    For more in-depth details and example, check Java 9 Modularity and Jigsaw Project with How to create Module in Java 9 with Eclipse.

  2. A New HTTP Client(HTTP/2 Client)

    A new HTTP Client implements HTTP/2 and WebSocket, and replace legacy the legacy HttpURLConnection API. The API will be delivered as an incubator module. It means the API isn’t guaranteed to be 100% final yet.

    The API will live under the jdk.incubtor namespace. 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 a lot of performance issues).

    HTTP 2 Client Example

    For more in-depth example, check OpenJDK HTTP/2 Client (Incubator).

  3. Process API Improvements

    The process API has been improved for controlling and managing operating-system processes.

    Two interface added and enhanced in Process API:

    1. java.lang.ProcessHandle class is enhanced to provide the operating specific process id of the process, information about the process, including the arguments, the command, the start time of the process, the accumulated CPU time of the process and the user name for the process
    2. java.lang.ProcessHandle.Info class returns information about each process as provided by the operating system, including process id, arguments, command, start time, etc. A ProcessHandle can return the process’ parent, and the direct children, and to all descendants via a stream of ProcessHandles.

    Process API Example

    The current method returns an object representing a process of currently running JVM. The Info subclass provide details about the process.

  4. JShell: the interactive Java REPL(Read-Eval-Print Loop)

    JShell is read–eval–print loop – REPL for short.

    It’s an interactive tool to evaluate declarations, statements, and expressions of Java, together with an API. It is very convenient for testing small code snippets.

    No more public static void main(String[] args) require to test sample snippet.

    The jshell executable itself can be found in <JAVA_HOME>/bin  folder:

    jdk-9\bin>jshell.exe | Welcome to JShell — Version 9 | For an introduction type: /help intro jshell> “I love codeNuclear”.toUpperCase(); $1 ==> “I LOVE CODENUCLEAR”

    The jshell tool will be a command-line tool with features to ease interaction including: a history with editing, tab-completion, automatic addition of needed terminal semicolons, and configurable predefined imports and definitions.

    For more information click on JShell with Example – Java 9

  5. Private methods in Interfaces

    Java 8 introduce default methods on 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 private keyword.

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

  6. Factory Methods for Collections

    Java 9 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 very tedious and verbose approach. With Java 9, several so-called collection factory methods have been added:

    Besides being shorter and nicer to read, these methods also relieve you from having to pick a specific collection implementation. In fact, the collection implementations returned from the factory methods are highly optimized for the number of elements you put in. That’s possible because they’re immutable: adding items to these collections after creation results in an UnsupportedOperationException.

  7. Improved Javadoc – Javadoc Search | HTML5 Support

    Add a search box to API documentation generated by the standard doclet that can be used to search for program elements and tagged words and phrases within the documentation.

    The search box appears in the header of all pages generated by the standard doclet.

    It also provides an option to the standard doclet to request either HTML 4 or HTML5 output.

    The HTML5 markup is semantic, i.e., clearly separating meaning from style and content.

    Javadoc Search
  8. 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 analyse an application’s static usage of deprecated APIs. They have add two methods to this Deprecated interface: forRemoval and since to serve this information.

    • forRemoval: Returning a boolean. If true, it means that this API element is earmarked for removal in a future release. If false, the API element is deprecated, but there is currently no intention to remove it in a future release. The default value of this element is false.
    • since: Returning String. This string should contain the release or version number at which this API became deprecated. It has free-form syntax, but the release numbering should follow the same scheme as the @since Javadoc tag for the project containing the deprecated API.

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

2 Thoughts on “8 New Features of Java 9”

Leave a Reply

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