Java 9: The Exciting Bits
Java 9 means a lot to different people, so what's got one dev excited about Java 9? The new HTTP client, immutable collections, and JShell top the list.
Join the DZone community and get the full member experience.Join For Free
Java 9 arrived last month, and its Java Development Kit (JDK) is available for download. It is packed with new features, including modularization, ahead-of-time compilation, a 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 WebSockets, which can replace the legacy HttpURLConnection API. The API is being moved to an 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.
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 a factory method for Lists:
List<String> list = List.of("apple", "banana", "orange");
As you can see, it’s very short and concise.
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, 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 an 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.
There are several improvements to Javadocs, 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.
Java 9 has a modular JVM and lots of other new improvements and features. Check out the complete list of features here.
Which features are you interested in? Let me know in the comments!
Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.