Java 9: The features I’m excited about

Java 9 is finally here, and its Java Development Kit (JDK) is available for download. It is packed with new features, including modularisation, ahead-of-time compilation, new HTTP client, HTML5 Javadoc, jshell, process API updates and so on.

I will list some of the features that I’m excited about.

A New HTTP Client

A new HTTP client API that implements HTTP/2 and WebSocket, which can replace the legacy HttpURLConnection API. The API is being moved to incubator module, which means that it will not be part of Java SE. It will reside under the jdk.incubtor namespace.

With this new API, performance should be on par with Netty and Jetty when used as a client API for HTTP/2. Also, memory consumption should be on par or lower than when using HttpURLConnection, Apache HttpClient, and Netty and Jetty when used as a client API.

Immutable Collections

Java 9 provides static factory methods on the collection interfaces that will create concise, unmodifiable collection instances. As per JEP 269, new convenience factory methods have been included in JDK 9.

In Java 8 creating a Set of several elements would require several lines of code. For example:

Set<String> set = new HashSet<>();

set.add("apple");

set.add("banana");

set.add("orange");

set = Collections.unmodifiableSet(set);

In Java 9, we can do it in just one line:

Set<String> set = Set.of("apple", "banana", "orange");

Similarly, we can use factory method for List:

List<String> list = List.of("apple", "banana", "orange");

As you can see, it’s very short and concise.

Compact Strings

The String class stores characters in a char array, using two bytes (16 bits) for each character. The problem is that strings are a major component of heap usage and, moreover, that most String objects can be expressed by just one byte using ISO-8859-1/Latin-1. Hence half of the space in the internal char arrays of such String objects is going unused.

Java 9 has enhanced String class that will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. There will be an encoding flag that will indicate which encoding to be used.

This new feature helps to optimize the performance and memory consumption on the JVM.

JShell Command Line Tool

The JShell (Read-Eval-Print Loop, as known as Java Shell) provides a way to interactively evaluate declarations, statements, and expressions of the Java programming language within the JShell state.

It is very convenient for testing small code snippets, which otherwise require creating a new class with the main method.

The JShell executable can be found within <JAVA_HOME>/bin folder.

Improved Javadoc

There are several improvements to the Javadoc and one of them is the addition of a search box. It also provides an option to the standard doclet to request either HTML 4 or HTML5 output.

Conclusion

Java 9 has a modular JVM and lots of other new improvements and features. Checkout the complete list of features here.

Which features are you interested in? Let me know in the comments!

 

Discussion