Since now Java 9 is released, lets look at some of the important Java 9 features that Oracle has released at the JavaOne event.
So, let’s get started.
Java 9 Features
#JShell: REPL
Oracle has joined the club of many languages which already have the feature of REPL [Read, Evaluate, Print, Loop]. The sole purpose of this feature is to accomplish easy test tasks of Java constructs like class, interface, enum, objects, statements, etc. Using this feature one can test tasks without the need to deploy the whole solution. JShell “Java Shell” can be executed from the terminal window. After running the JShell one can directly start typing and execute java code.
The good part of using this tool is that now we can test our microservices without the need to deploy them over and over. This helps in accelerating the development, evolution, and quality of the final components.
#Private methods in Interfaces
Java 8 provided us with the Default and Static method implementation in Interfaces; however, we cannot create private methods in interfaces. If we are in a situation where we have multiple default methods in an interface containing the same functionality. In this scenario normally we would refactor these default methods to call a private method containing the shared functionality. In Java 8 you can create private methods in Interfaces. But with the release of Java 9, this problem set has been answered and now we can create private helper methods in Interfaces to solve this type of problems.
public interface MyInterface { default void interfaceDefaultMethod() { init(); } default void interfaceSecondDefaultMethod() { init(); } // This is the declaration of the Private Method in Interface private void init() { ... } }
#Java 9 Module System
One of the biggest change with Java 9 is the introduction of the Module System (Jigsaw Project). Till the Java 8 version we were using the monolithic jars to design our Java based applications. There are two main fundamental problems with this design: Hard to encapsulate code and there is no notion of explicit dependencies between different parts of a system. To avoid these fundamental problems Java 9 has introduced the Module System. Here are some of main benefits that we get from the Module System.
- The current JDK, JRE, JARs, etc are now divided into smaller modules so we can use only the modules that we want. This helps in scale down the application for smaller devices.
- Strong support for encapsulation.
- Internal Non-Critical APIs are hidden now as Modules hide unwanted and internal details very safely. This improves the security of the application.
#Javadoc Improvement
We all use Google to search for problems and find the right Javadoc pages. With Java 9 that problem has been solved. Java 9 now supports the API documentation search internally within the Javadoc.
#HTTP/2 Client
A new HTTP/2 Client API has been released to support HTTP/2 protocol and websockets features. This is a replacement to the old “HttpUrlConnection”.
HttpClient newHttpClient = HttpClient.newHttpClient(); Uri uriObject = Uri.create("http://developersjournal.in"); HttpRequest request = HttpRequest.newBuilder(uriObject) .header("User-Agent", "DJ") .GET() .build(); HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandler.asString());
#New Version-String Scheme
Provides a simplified version-string format that helps to clearly distinguish major, minor, security, and patch update releases.
The new version-string format align with current industry practices, in particular Semantic Versioning (Server). The format is as follows:
$MAJOR.$MINOR.$SECURITY.$PATCH
$MAJOR
is the version number that is incremented for a major release, for example JDK 9, which contains significant new features as specified by the Java SE platform specification. A major release contains new features and changes to existing features, which are planned and announced well in advance.$MINOR
is the version number that is incremented for each minor update, such as bug fixes, revisions to standard APIs, or implementation of features outside the scope of the relevant platform specifications.$SECURITY
is the version number that is incremented for a security-update release, which contains critical fixes, including those necessary to improve security.$PATCH
is the version number that is incremented for a release containing security and high-priority customer fixes that have been tested together.
#Multi-Release JAR files
Extends the JAR file format to enable multiple, Java release-specific versions of class files to coexist in a single archive.
A multirelease JAR (MRJAR) contains additional, versioned directories for classes and resources specific to particular Java platform releases. Specify versioned directories with the jar
tool’s --release
option.
#Improvement in the Stream API
The improvement in the Stream API allow incoming streams of object-serialization data to be filtered in order to improve both security and robustness.
Object-serialization clients can validate their input more easily, and exported Remote Method Invocation (RMI) objects can validate invocation arguments more easily as well.
Serialization clients implement a filter interface that is set on an ObjectInputStream. For RMI, the object is exported through a RemoteServerRef that sets the filter on the MarshalInputStream to validate the invocation arguments as they are unmarshalled.