Java 9 is coming soon. Are you ready for the new features the new version will bring? In this article, I give a comprehensive overview of some of the important changes to the Java ecosystem, including new module systems, language and syntax changes.
As the release date for Java 9 nears, interest in the new features it will bring is growing among programmers and developers. The release date for the new Java version is September 21, 2017. Less than two months away! There are many changes in Java 9, and I’ll list nine important improvements that have been added to the new Java 9.
1. New module system
There are several problems with writing large applications or maintenance libraries. As the code base increases, the opportunity to create complex code increases. It is difficult for every user to truly encapsulate code classes as part of a common API, and there is no clear concept of dependencies between different parts of the system.
Jigsaw, included in the new Java version, aims to address all of these issues. A module will consist of the usual classes and a new module declaration file. This module descriptor clearly defines what dependencies our module needs and which modules are used externally. All packages not mentioned in the exports clause are wrapped in modules by default.
A simple module declaration exports some of its packages:
If you want to learn more about how to build application modules or become familiar with more examples of Jigsaw syntax and projects, you can visit the Quick Start guide here.
http://openjdk.java.net/projects/jigsaw/quick-start
2. Support HTTP / 2.0
The main difference between HTTP/1.1 and HTTP/2 is how data is built and transferred between the client and server. HTTP/1.1 relies on the request/response cycle. HTTP/2 allows the server to “push” data: it can send more data than the client requests. This allows it to prioritize and send data that is critical to loading the web page in the first place. Java 9 will fully support HTTP 2.0 and provide Java with a new HTTP client that will replace HttpURLConnection for blocking mode only — there is one thread per request/response, which increases latency and load time for web pages. The HTTP client also provides apis to handle HTTP functions such as HTTP and server push.
Two examples of HTTP interaction from the Java 9 documentation are shown below:
3. Improved Javadoc
Based on my experience working at Dreamix, a Java development company. Currently, if you want to find some class documents, you have to do a Google search. In Java 9, Javadoc has several improvements, one of which is the addition of a search box.
This is shown in Java 8
This is shown in Java 9
4. The Stream improvement
The Stream API is one of the game-changing features in Java 8, and Java 9 has gotten better. Now you can create Stream from Optional. The Stream interface also added four new methods: iterate, dropWhile, takeWhile, and ofNullable.
DropWhile discards the Stream’s first item until the condition is met.
TakeWhile processes the item until the condition is satisfied.
Iterate allows you to write appropriate replacements for the for loop using Stream. It requires an initial value for the Stream, a condition that defines when to stop iterating, and a step function that generates the next element.
OfNullable as a name suggestion lets you create streams from objects without checking for NULL. It returns a sequential Stream containing a single element, or an empty Stream if non-empty.
5. Initialize Collections more easily with the new factory method
Currently, to create a list of predefined values, you have to do a lot of typing:
In the future, the initialization of common Collections will be easier with the newly added factory methods. Static methods in the interface make this possible, enhancing the list, collection, and mapping interfaces to create Collections methods with up to 10 elements. The generated objects are immutable Collections optimized for performance.
After creating these Collections to add items will lead to “UnsupportedOperationException”.
The code above looks nicer:
Here are some ways to add:
6. Private methods in interfaces
Java 8 gives us default methods in the interface. These methods have the body and behavior of giving the interface, not just an empty signature. If you had two public methods that were almost identical, what would you do? You will most likely try to move generic code privately and call it from a public method. But in a similar case, would you use two default methods in an interface instead of two public methods in a class?
In Java 9, you can use exactly the same method, with a private method with general logic that will not be part of your API.
7. Language and grammar improvements
It will now be easier to write a try with a resource statement. Previously, all resources that had to be closed after execution had to be initialized in the try clause, as shown in the following example:
Starting with Java 9, we can use final and valid final resources in the try clause:
From Java 9 variable names cannot consist of a single underscore (” _ “). It is possible to underline variable names in my_var, but a single underscore will result in an error. The reason behind this is that underlining will be reserved for future use in the language.
We will be able to use the diamond operator with the anonymous inner class:
8. Enhance processing apis
Until now, the ability to manage and control operating system processes has been limited. Furthermore, the code you write to perform such interactions depends on the operating system.
The new version will extend the ability to interact with the operating system. New methods will be added to handle PID management, process name and status, child process management, and more.
Sample code to retrieve the current process PID and run on all operating systems would look like this:
9.Java REPL = Jshell
Last but not the least important Java9 Kulla will include project (http://openjdk.java.net/projects/kulla/) in the Read the Evaluate Print Loop (REPL) tools. This command-line tool is called JShell, and it’s perfect if you want to write a few lines of your own code to test it out.
Simple commands do not require a new class using the main method.
What don’t we see in the new version?
Several good features have been removed from the upcoming release. However, you can wait in Java 10.
A standardized and lightweight JSON API is favored by many Java developers. This will not be seen in Java 9 due to funding issues, but it will not be cut. “This JEP will be a useful addition to the platform,” said Mark Reinhold, lead architect for the Java Platform, in the JDK 9 mail column, “but it is not planned to be as important as other features funded by Oracle and may be reconsidered for implementation in JDK 10 or later.”