Features of Java SE 9

Introduction

The release, Java 8, was filled with several new features that had a significant impact on Java application development. Before JDK 8, every JRE had all the classes to run in a single Java archive (JAR) called rt.jar, which was present in the lib directory.

This led to a large number of unnecessary classes being added to the JVM at runtime, which overloaded the JVM. Thus, the JAR becomes unwieldy in size, and the runtime environment is forced to deal with all the classes, whether they are used or not.

But Java SE 9 comes with more versatility and customization, as well as many new features. Most important is the Jigsaw project, which implements a new modular system for separating JREs into reusable and compatible components.

The program code is divided into modules containing tasks to be executed. This makes programs reusable and easier to manage and debug. Another advantage of modularity is that it makes the Java platform lighter and more scalable, so that programs can run on smaller devices with fewer resources.

The modular JRE can even be used in embedded systems, allowing IoT applications to be coded using Java.

Features of Java SE 9

Java Platform Module System (JPMS)

This is the most important feature of Java SE 9. The important problems encountered in the growth of the code base of the system,

  • The complexity of code encapsulation.
  • Lack of clarity in the dependencies between the various aspects (JARs).
  • Easy access to public classes by classspath, leading to careless use.
  • Lack of confidence in the availability of all required JARs on the classpath.

All these issues are solved in a modular system through the use of a module descriptor in modular JARs. In this file, the module can define a list of all other modules on which the use of the required operators depends.

In addition, the export keyword is used to make module packages available to other modules. If the export keyword is not used with a package, it is encapsulated in this module by default. Note that module mod1 requires module mod2.

Both modules have packages that are encapsulated (as indicated by the screen symbol) because they are not exported. This ensures that the package classes are not accidentally accessed.

Programmatically, this can be represented as shown below,

module mod1 {
  exports com.mod1;
  requires mod2;
}

Similarly, the entire Java platform has been modularized by encapsulating the internal JDK classes, making it more secure and flexible.

Unlike JAR, the JVM checks the existence of all dependent modules specified in the “Requirement” statement in the classpath. This avoids unexpected errors and code locking at runtime, which have been encountered in the past due to missing JARs.

Linking

Minimum runtime can be created by adding only those modules that are needed to run the application. This can be done with the new jlink tool that comes with Java 9.

The modularized JDK and the module descriptor file containing the explicit dependencies mentioned in it allow to create this minimum runtime. This way you can create minimal and optimized runtime and avoid cumbersome JDK installation.

JShell

JShell is the new Read-Eval-Print-Loop (REPL) interactive terminal in JDK 9. You can run jshell from the console and write and execute Java code directly from it.

When you run the code, it gives you immediate feedback that allows you to test and learn new APIs and language features. You can also test regular expressions in Java using the interactive shell.

Enhanced Javadoc

Javadoc now offers an integrated search function in the API documentation. In addition, the new Javadoc is HTML5 compatible and each page contains information about the JDK module to which the class or interface belongs.

Collection Factory Methods

Java SE 9 brings a new feature with factory methods for collections such as List, Set, etc. This helps the developer quickly populate collections in a single statement instead of using multiple add calls. For example,

Set<Integer> set1 = Set.of(3,4,5);

These collections are immutable, meaning that no items can be added to these collections after they have been created, otherwise an UnsupportedOperation exception will be thrown. Using factory methods makes the code shorter and avoids the specific implementation section of the collection type.

Enhancements in Stream API

Java SE 9 adds more functionality to the Stream API by allowing declarative transformations on collections. It adds new methods to the Stream interface, namely dropWhile, takeWhile, ofNullable, and provides an overloaded version of the iteration method to provide an expression to stop iteration.

The lambda expression in the second argument returns true until the number is 100 and outputs integers from 1 to 99.

Private interface methods

Java SE 8 introduced the concept of default methods for interfaces. But interfaces only allowed method signatures without any implementation.

With Java SE 9, interfaces can also have behavioral methods and private helper methods to implement generic code that can be called from multiple default methods. This is explained by the following code,

public interface Interface1 {
  void interfaceMethodWithOnlySignature();
default void interfaceDefaultMethod() {
    initProcess();
  }
default void interfaceDefaultMethod1() {
    initProcess();
  }
  // Private method that is not part of the public API exposed by Interface1 
  private void initProcess() {
    System.out.println(“Initializing the Process”);
  }
}

Therefore, private interface methods can help structure the implementation of the API’s with default methods.

HTTP/2

Java 9 introduces a new way to make HTTP calls with the HTTPClient API, replacing the old HttpURLConnection API, which also supports WebSockets and HTTP/2.

HttpClient client = HttpClient.newHttpClient();

Multi-release JARs

Java SE 9 implements the multi version JAR’s feature as a solution to cumbersome JARs resulting from legacy libraries saved for backward compatibility.

This feature allows you to create alternate versions of classes that will only be used when the library runs on a particular version of Java.

Leave a Comment