What is new in Java 9?

Sep 19, 2017

robosoft technologies
robosoft technologies
What is new in Java 9?

Next major version of Java programming language is scheduled for general availability on 21 September, 2017. It is Java 9 (Java SE 9 Platform, JDK 9). Though this new version does not have as paradigm changing features as Java 5 or Java 8, it has got many interesting additions to the language, which has been the venerable workhorse of software development.

Some of the noteworthy new features are :

jshell: The Java Shell (Read-Eval-Print Loop)

We now have a REPL (Read-Eval-Print Loop) for Java language called jshell. A REPL is a simple, interactive computer programming environment that takes single user inputs (i.e. single expression), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise. REPL is a very handy tool for teaching programming language and learning the language quickly. REPL in dynamically typed, interpreted languages like Ruby, Python make them easier to learn and experiment with language features. Immediate feedback is important when learning a programming language and its APIs. With the advances in compiler technologies, even the statically typed, compiled languages like Swift and Scala have REPL shells.

Previously in Java, to get a simple “Hello World” printing program up and running, one needed to write lot of ceremonious, boiler plate code. The problem for the instructor was that he or she needed to explain away the distracting boiler plate code while teaching simple expressions.

Jshell provide an interactive tool to evaluate declarations, statements, and expressions of the Java programming language. Also, Java 9 has an API so that other applications like IDEs can leverage this functionality. To facilitate rapid investigation and coding, statements and expressions need not occur within a method, and variables and method need not occur within a class. The tool has a history with editing, tab-completion, automatic addition of needed terminal semicolons and configurable predefined imports and definitions.
Exploration of coding options is also important for developers prototyping code or investigating a new API. Interactive evaluation is vastly more efficient in this regard than edit/compile/execute and System.out.println.

Module System

Java 9 incorporates design and implementation a standard module system for the Java SE Platform and applies that system to the Platform itself under a project codenamed Jigsaw. Now, the javac compiler, the HotSpot virtual machine, and the run-time libraries will implement modules as a fundamental new kind of Java program component and provide for the reliable configuration and strong encapsulation of modules.

Modules solve the problem large runtime jars like rt.jar that every java program needed to carry along and allow to use only subcomponents our program is dependent on. Modules allow us to define internal APIs that internal to a module. JDK is no longer one big blob of jar, but is composed smaller modules. The module graph of JDK is shown in below figure:

The module graph of JDK

Image Source: Openjdk

Module is a collection of code (packages) and data, it has a name, tells what it needs (requires) and tells what it provides. It should not have cyclic dependencies. Modular JAR files contain an additional module descriptor. In this module descriptor, dependencies on other modules are expressed through`requires` statements. Additionally, `exports` statements control which packages are accessible to other modules. All non-exported packages are encapsulated in the module by default.

When starting a modular application, the JVM verifies whether all modules can be resolved based on the `requires` statements—a big step up from the brittle classpath. Modules allow you to better structure your application with strong enforcement of encapsulation and explicit dependencies.

SHA-3 Hash Algorithms

JDK implements the SHA-3 cryptographic hash functions (BYTE-only) specified in NIST FIPS 202.

FIPS 202 defines four new hash functions: SHA3-224, SHA3-256, SHA3-384, and SHA3-512. These can be implemented as new algorithms of the java.security.MessageDigest API under the standard names “SHA3-224”, “SHA3-256”, “SHA3-384”, and “SHA3-512”. Here is the list of providers and the corresponding algorithm enhancements:

  • “SUN” provider: SHA3-224, SHA3-256, SHA3-384, and SHA3-512
  • “OracleUcrypto” provider: SHA-3 digests supported by Solaris 12.0

Process API Updates

Java SE provides limited support for native operating-system processes. It provides a basic API to setup the environment and start a process. Java 9 adds several new methods to the abstract Process class that let you identify direct child or descendent processes, obtain this Process’s PID, return a snapshot of information about this Process, obtain a completable future to receive asynchronous notification when this Process exits, and more.

HTTP/2 Client

JDK 9 define a new HTTP client API that implements HTTP/2 and WebSocket, and can replace the legacy HttpURLConnection API. Besides this simple request/response model, HttpClient provides new APIs to deal with HTTP/2 features such as streams and server push.

To make a request it is as simple as getting your client, building a request and sending it as shown below.

HttpClient httpClient = HttpClient.newHttpClient(); //Create a HttpClient
HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("https://www.google.com/")).GET().build(); //Create a GET request for the given URI
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString()); //Send the request that will decode the body as a String
System.out.println(httpResponse.body()); //Output the body of the response

For comparison here is the same functionality in the current API.

URL url = new URL("https://www.google.com/"); //Specify the URL
URLConnection urlConnection = url.openConnection(); //Create the connection
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Create input stream and load into reader
String inputLine;
while ((inputLine = reader.readLine()) != null) { //Loop through and output each line in stream.
reader.close(); //Close Reader

Some of the popular demands that did not make into JDK 9 are reified generics, full fledged type inference in the language and JSON API. For complete list of changes in JDK 9, we can refer Open JDK page at http://openjdk.java.net/projects/jdk9/


1. http://openjdk.java.net/projects/jdk9/
2. https://www.youtube.com/watch?v=8XmYT89fBKg

H N Ramkumar H N Ramkumar H N Ramkumar is a Technical Architect at Robosoft and has led the design & developments of many projects across Mac and iOS
robosoft technologies
robosoft technologies