Java 9

Overview of new features


03 Feb 2018 View Comments
#java9 #java #computers #programming #Jigsaw

Java 9 has been finally released in September 2017. It has been delayed a couple of times already well over 3 years. There are approximately 90 new changes/enhancements released as part of the Java 9. According to the JDK Enhancement Proposal JEP 322, next Java version is planned to be Java 10. See diagram below for the reference to the upcoming Java releases in the future.

Java release plans
Java release plans

Please keep in mind that the schedule is roughly scheduled and not accurate. This diagram is mainly showing the versioning change in Java 9 along with the plans. Anyways, let’s take a look at some of the updates in Java 9.

By the way, there are 2 important acronyms you need to understand before we dive into it.

  1. JSR - Java Specification Requests
    includes change requests for the Java language, libraries and other components.
  2. JEP - Java Enhancement Proposal
    includes enhancements proposals for the Java Development Kit and OpenJDK.

As you read the overview below, you will encounter these names with numbers quite often. For example, with JSR 376, you can read the formal documents from either Openjdk or Oracle. As I mentioned above, all the JEPs discussed here are listed under 90 new changes/enhancements: For example, JEP 222, JEP 102, JEP 110, JEP 165 etc.

1. The Java Platform module system

Project Jigsaw - JSR 376

Here is probably the most important change featured in Java 9, The Java Platform Module System. It is developed under Project Jigsaw which comes to us with the specific goal: to provide reliable configuration and strong flexible encapsulation. Currently, every public class can be accessed by any other public classes on the classpath, leading to inadvertent usage of classes that weren’t meant to be public API. Furthermore, the “classpath hell” itself is problematic because it assumes that all JARs are available and non-duplicated. By providing a reliable configuration, we can avoid using this CLASSPATH mechanism to load the configurations. Strong encapsulation means that a class would recognize which public types are used appropriately. With these changes, it provides a scalable platform for improved performance.

Overall what this means at the end of the day is the new JDK has been divided into a set of modules. Programmers can now combine these JDK modules into a variety of configurations meaning the programmers are getting what they really need. They can run java --list-modules to see the list of available modules. java.base is probably one of the most important modules in the list. It includes all the libraries we are used to using like java.util, java.lang, etc.

So why do we need to modularize? It allows us to build a flexible (by loose coupling) and scalable (small devices to a cloud environment) application which also fairly improves the performance and security as well.

For the reference, there are at least 4 important JEPs contained in this JSR 376:

  1. JEP 200- The Modular JDK: Modularizing JDK itself.
  2. JEP 220- Modular Run-Time Images
  3. JEP 260- Encapsulate Most Internal APIs
  4. JEP 261- Module System itself: Big piece of the puzzle, really.

2. JShell - Java REPL

JEP 222

There is Read-Evaluate-Print-Loop (REPL) available for most of the modern languages these days including, Python and Scala. REPL is basically an interactive shell where you can run the program inside a command line tool. They are generally useful in evaluating declarations, statements and expressions which could be used to confirm many of your algorithms. It is also a great tool to explore new coding options when you are developing a new one. Some features including: a history with editing, tab-completion, automatic addition of needed terminal semicolons, and configurable predefined imports and definitions. If you are looking for an immediate feedback on your code, REPL is the way to go!

3. Process API Updates

JEP 102

Java had been relying on the native code to execute process related calls, here are some updates to the new Java 9:

  • The update extends Java’s ability to interact with the OS
  • ProcessHandle class provides access to critical data
  • Native processor ID, arguments, command, start time, CPU time, user

For example, here is a little comparison of getting a processor ID in earlier Java vs Java 9:

Earlier Java using JNA:

public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);
int getpid ();
}

then use the following call to get the PID:

int pid = CLibrary.INSTANCE.getpid();

However, in Java 9, we can use ProcessHandle class which makes everything simpler.

long pid = ProcessHandle.current().getPid();

4. HTTP/2 Client (Incubator)

JEP 110

In 2015, HTTP/2 was standardized. The HTTP/2 protocol itself features a lot more advantages than the earlier HTTP/1. Here are some new features in HTTP/2:

  • bidirectional communication using push requests
  • multiplexing within a single TCP connection
  • long running connections eventually fewer connections, less load
  • stateful connections

Anyways, HTTP/2 brings a better performance overall. I will cover the HTTP/2 topic in the future blog in detail.

In JDK 9, this is handled through HttpClient class which handles creation and send requests. This is sitting in the package of jdk.incubator. The word incubator is used because the API and implementation will not be part of Java SE. The package will not be loaded by default at compile- or run-time. With the latest JDK 9, You will have to load it separately using the –add-modules command-line option to request that an incubator module is resolved, like this:

java -jar --add-modules jdk.incubator.httpclient sample-run.jar

Conclusion

There are many other features (JEPs) added to JDK 9 which improves security and performance overall. They are all in the 90 new changes/enhancements of JDK 9. It is worth your time to read the list to see what changes are happening in Java 9.

Share this post

Me

I am a passionate software developer working in Downtown, Vancouver. I strongly believe in art of algorithms and together with it to write clean and efficient software to build awesome products. If you would like to connect with me, choose one from below options :) You can also send me an email at