In our previous blog , we discussed the features that were added in Java 8.
More than three years after the release of Java 8, the next version is now just around the corner with a tentative release date of September 21st, 2017.
You may have heard about Java 9’s module system, but there’s even more to this new version. In this blog, We will try to cover some of them.
One can download JDK 9 EA (Early Access) software from Java 9 Download
So without wasting any time Let’s Begin with the New features in Store :
Java 9 REPL(JShell)
Oracle Corp has introduced a new tool called “jshell”. It stands for Java Shell and also known as REPL (Read Evaluate Print Loop). It is used to execute and test any Java Constructs like class, interface, enum, object, statements etc. very easily. Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. The Repl/jshell looks like :
anmol:\>jshell Welcome to JShell -- Version 9-ea For an introduction type: /help intro jshell> System.out.println("Hi All") Hi All
No More `public static void main(String[] args)`. Cool Right?
HTTP 2.0 Client
Java 9 will have full support for HTTP 2.0 and feature a new HTTP client for Java that will replace HttpURLConnection, and also implement HTTP 2.0 and websockets.
The new API is located under the java.net.http package.
This API uses the Builder pattern, which makes it really easy for quick use :
URI newURI = new URI("http://localhost:8080"); HttpRequest request = HttpRequest.create(newURI).GET(); HttpResponse response = request.response(); String responseBody = response.body(HttpResponse.asString());
Java 9 Modularity(Jigsaw Project)
One of the big changes or java 9 feature is the Module System. Oracle Corp is going to introduce the following features as part of Jigsaw Project.
- Modular JDK
- Modular Java Source Code
- Modular Run-time Images
- Encapsulate Java Internal APIs
- Java Platform Module System
One of the main motivations here is to provide modular JVM, which can run on devices with a lot less available memory. The JVM could run with only those modules and APIs which are required by the application.
The module system will look somewhat like :
module java9 {
exports ;
requires ;
}
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.
You can explore more about the jigsaw project : here
Process API Improvements
Java SE 9 is coming with some improvements in Process API. They have added couple new classes and methods to ease the controlling and managing of OS processes.
Two new interfaces in Process API:
- java.lang.ProcessHandle
- java.lang.ProcessHandle.Info
This update will extend Java’s ability to interact with the operating system: New direct methods to handle PIDs, process names and states, and ability to enumerate JVMs and processes and more. For example , The Code for retrieving Linux PIDs will look like :
System.out.println(" Required PID is " + Process.getCurrentPid());
Lightweight JSON API
Java 9 comes with a light-weight API for consuming and generating documents and data streams via the JavaScript Object Notation data interchange format, which is based on a subset of JavaScript and serves as an alternative to XML.
The main goals of this API will be as follows :
- Parsing and generation of JSON .
- Functionality meets needs of Java developers using JSON.
- Parsing APIs which allow a choice of parsing token stream, event (includes document hierarchy context) stream, or immutable tree representation views of JSON documents and data streams.
- Useful API subset for compact profiles and Java ME.
- Immutable value tree construction using a Builder-style API.
- Generator style API for JSON data stream output and for JSON “literals”.
- A transformer API, which takes as input an existing value tree and produces a new value tree as result.
It is expected that this JEP will be delivered as a set of new sub-packages in the java.util
hierarchy and will consist of at least four modules; events, streams, trees, generators. No existing modules, packages, or classes are expected to be modified. The JSON API is not expected to have any dependencies that are not already part of the Java base module.
Smart Java Compilation (Phase 2)
The Smart Java Compilation Project is intended to improve the sjavac
(Java programming language compiler) tool enabling it to be used by default in the JDK build and also to generalize it so it can be used to build large projects other than the JDK.
Due to various issues relating to stability and portability, sjavac
is not used by default in the JDK build scripts. The first goal of this JEP is to resolve these issues. This involves making sure the tool produces reliable results on all software/hardware configurations at all times.
The overarching goal is to improve the quality of sjavac
to the point where it can serve as a general purpose javac
wrapper able to compile large arbitrary Java projects.
Private interface methods
In Java 8, we can provide method implementation in Interfaces using Default and Static methods. However we cannot create private methods in Interfaces.
To avoid redundant code and more re-usability, Oracle Corp is going to introduce private methods in Java SE 9 Interfaces. From Java SE 9 on-wards, we can write private and private static methods too in an interface using ‘private’ keyword.
A private method can be either static so it belong to an interface. And a private method without static
, and that can only be called on a instance of the interface but only within the interface.
public interface Employee{ private Long createEmployeeID(){ // Method implementation goes here. } private static void displayEmployeeDetails(){ // Method implementation goes here. } }
If you’re evolving APIs with default methods, private interface methods can be helpful in structuring their implementation.
Reactive Streams
Now-a-days, Reactive Programming has become very popular in developing applications to get some beautiful benefits. Scala, Play, Akka etc. Frameworks has already integrated Reactive Streams and getting many benefits. Oracle Corps is also introducing new Reactive Streams API in Java SE 9.
The class java.util.concurrent.Flow
encloses the following 4 interfaces
These interfaces support the Reactive Streams publish-subscribe framework. Java 9 also provides a utility class SubmissionPublisher
. A Publisher produces items that are consumed by a number of Subscribers. And a Subscriber is managed by a Subscription. Subscription links the Publisher and Subscriber.
Мulti-Resolution Image API
In Java SE 9, Oracle Corp is going to introduce a new Мulti-Resolution Image API. Important interface in this API is MultiResolutionImage .
MultiResolutionImage encapsulates a set of images with different Height and Widths (that is different resolutions) and allows us to query them with our requirements.
Basically this new API allows a set of images with different resolutions (width and height) to be encapsulated into only one single image.
The new API which is defined in the java.awt.image
package can help us:
- Encapsulate many images with different resolutions into an image as its variants.
- Get all variants in the image.
- Get a resolution-specific image variant – the best variant to represent the logical image at the indicated size based on a given DPI metric.
Java 9 provides a MultiResolutionImage
Interface with 2 important methods getResolutionVariant()
that returns an Image and getResolutionVariants()
that returns a list of Image
s:
package java.awt.image;
public interface MultiResolutionImage {
Image getResolutionVariant(double destImageWidth, double destImageHeight);
public List getResolutionVariants();
}
Apart from these features , There are Loads of new features/improvements in store with the release of Java 9 like :
- GC (Garbage Collector) Improvements
- HTML5 Javadoc
- Stream API Improvements
- Enhanced @Deprecated Annotations
- Optional Class Improvements
- CompletableFuture API Improvements etc.
Well, all said , all these features are expected to make their way into the Java 9 update but we can say nothing is ever guaranteed when it comes to Java.
So Let’s Wait and See What Java 9 has in store for us.
Hope This blog helps. Stay Tuned for More 🙂
Reblogged this on jyotsnajot.
Reblogged this on Anmol Sarna and commented:
Did Someone Say Java 9 ??
One of the big wins that utilizes jigsaw is jlink. You can package a slimmed-down version of the jre with your application. Using the highest compression level option, a hello world application clocks in at 10MB. If I can find an example I will add it to this comment, but I need to get back to work 😛
Thanks, nice post
Reblogged this on Site Title.
Reblogged this on Coding, Unix & Other Hackeresque Things.