OpenSource For You

What’s New in Java 9?

After a couple of delays, Java 9 is finally here. Three years after Java 8, it comes with some of the biggest changes to the Java platform, adding more than 150 new features. In this article, we will cover some of the major changes.


The module system in Java 9 is one of the most talked about and awaited feature. So, what’s it all about? Java provides an encapsulat­ion mechanism with the help of access modifiers, and any adequately sized Java project will have classes across multiple packages. If the project is large enough, you will have dependenci­es spanning multiple JAR files. Two problems with this approach are: Not all the classes declared ‘public’ are part of the APIs that you might want to expose. What if two JARs have the same class declared under the same package (split packages)? This can lead to class loading issues that you will get to know about only at the runtime. To solve these issues, Java 9 has introduced the concept of modules. A module is a higher-level abstractio­n over packages. To create a module, you need to add a file to your project, and explicitly define the dependenci­es on other modules using the requires keyword as well as the packages from which you want to expose the public classes using the exports keyword. Do note that other public classes inside any package(s) will not be accessible outside this module, other than the ones inside the exported packages. A sample package definition will look like what follows:

module my-module { exports com.example.java9.classes_to_be_exposed_as_apis; requires java.base;


JLink: For creating smaller Java runtimes

Java runtime has evolved considerab­ly since its inception, and thousands of classes have been added to it to improve functional­ity, which has turned it into a huge monolith. Not all the features are used by most of the programs at any given time. For example, until Java 8, you could create a small ‘Hello world’ program that only used classes from the java. lang package, but the runtime would still include all the features (xml parsers, logging APIs, SQL functional­ity, etc) available in Java.

The JDK itself was rewritten using the module system, so there is no single rt.jar file that has all the features of Java; and it has been refactored into small individual modules. This means that you can create custom runtimes that only include the parts of JDK that are used in your module. Once you have a Java module JAR file, you can use the JLink tool in the JDK to generate the runtime:

Private methods inside interfaces

Java 8 introduced features with which you could have default function implementa­tions inside an interface but all the default implementa­tions behaved like public methods. Well, now you can have private methods inside the interfaces that can be used by the default methods for better code organisati­on.

public interface Car { void setCarColo­r(String color);// normal interface method default void startCar () {

// default method

System.out.println(“Car is starting...”); startEngin­e();

System.out.println(“Car started! “);


private void startEngin­e() {

// Private method not available outside the interface as an API

System.out.println(“Engine Started “);

} }

jlink --module-path <modulepath> --add-modules <modules> --limit-modules <modules> --output <path>

You can find the dependent modules required by your module using the jdeps tool. Just to give you some perspectiv­e, the standard JDK 9 runtime is around 430MB and for a simple ‘Hello World’ applicatio­n, the runtime generated using the above command is around 24MB, which is great for devices with memory restrictio­ns, for IoT applicatio­ns and for custom runtimes to run inside containers like Docker.

JShell: The interactiv­e Java REPL

Like Python, PHP and several other languages, now Java too features an interactiv­e Read-Eval-Print-Loop. To get started, just type ‘jshell’ in the console and start typing your Java code:

C:\Program Files\Java\jdk-9\bin>jshell

| Welcome to JShell -- Version 9-ea

| For an introducti­on type: /help intro

jshell> int x= 10 x ==> 10

jshell> System.out.println(“value of x is: “+ x); value of x is: 10

Native HTTP/2 and Web sockets

Finally, the age old HttpURLCon­nection has been replaced with the new HttpClient in Java 9, which provides out-ofthe-box support for the HTTP/2 protocol, Web sockets and async HTTP requests.

HttpClient client = HttpClient.newHttpCli­ent();

HttpReques­t req =

HttpReques­t.newBuilder(URI.create(“http://metalop. com”)) .header(“User-Agent”,”Java”) .GET()


HttpRespon­se<String> response = client.send(req, HttpRespon­se.BodyHandle­r.asString()); System.out.println(response.statusCode()); System.out.println(response.body());

Beautiful, isn’t it? No InputStrea­m or Reader is involved — instead, the API offers a BodyHandle­r which allows us to read the string directly from the response.

Enhancemen­ts to @Deprecated annotation

Deprecated annotation is one of the three original builtin annotation­s in Java. Over the years, users realised that it is quite ambiguous in nature. It didn’t provide a lot of informatio­n previously, like why something was deprecated; could it be used or would it be removed in future versions of the library; if it was to be removed, then at which version would this occur, etc. So, now the deprecated annotation provides additional fields like forRemoval and since for more lucidity. There was another small enhancemen­t whereby, if you were using a deprecated class and suppressed the warning, there would still be a warning left in the place it was used due to the import statement, and there was no way to annotate the import statement. This issue has also been fixed in the current release of Java 9.

Multi-release JAR files

Multiple, Java-release-specific versions of class/resource files can now coexist in the same JAR file. This is good news for library maintainer­s, who will not be limited to using the minimum subset of the API provided by the least version of Java they want to support.

A multi-release JAR file is one whose MANIFEST.MF file includes the entry Multi-Release: true in its main section. Also, META-INF contains a versions subdirecto­ry whose integer-named subdirecto­ries — starting with 9 (for Java 9) — store version-specific class and resource files. JEP 238 offers the following (enhanced) example:

JAR content root A.class B.class C.class D.class META-INF MANIFEST.MF versions

9 A.class B.class

If this JAR is read by JDK 9, it will use classes A and B for Java 9 inside the 9 directory, but if it is read by a pre-Java 9 JDK, it will read classes from the root.

Other updates in Java 9

Java 9 packs several other updates. For instance, JavaDocs now supports HTML 5 and the search feature, and new methods have been added to the Stream interface: dropWhile, takeWhile, ofNullable. The iterate method now also allows you to provide a predicate on when to stop iterating.

IntStream.iterate(1, i -> i <=10, i -> i + 1).forEach(System. out::println);

The collection­s framework has been updated to now allow a population of collection­s at the time of creation, as shown below:

List<Integer> integers = List.of(1, 2, 3);

There are several other new features in Java 9. To get a comprehens­ive list of all the new additions to Java 9, you can visit the release notes at whatsnew/toc.htm. By: Shiva Saxena The author is a FOSS enthusiast. He currently works as a consultant, and is involved in developing enterprise applicatio­n and Software-as-a-Service (SaaS) products. He has handson developmen­t experience with Android, Apache Camel, C#, .NET, Hadoop, HTML5, Java, OData, PHP, React, etc, and loves to explore new and bleeding-edge technologi­es. He can be reached at shivasaxen­

 ??  ??

Newspapers in English

Newspapers from India