Java 9 is almost here, and with its approaching release date, interest in the new features it will bring is at an all-time high. The current release date for the new Java version is 21 September 2017. That means we don’t even have two months to go! In order to prepare for using these new features, it’s a good idea to take a look and get a feel for how Java 9 really works.
There’s a lot of changes with Java 9. Below, I will list the top 9 improvements that have made it into the new Java 9. Let’s dive right in!
Contents
- 1 1. New module system
- 2 2. HTTP/2.0 support
- 3 3. Improved Javadoc
- 4 4. Stream improvements
- 5 5. Easier initialization of collections with new factory methods
- 6 6. Private methods in interfaces
- 7 7. Language and syntax improvements
- 8 8. Enhanced process API
- 9 9. Java REPL = Jshell
- 10 What we will not see in the new release?
1. New module system
There are several problems with writing large applications or maintaining a library. With the increase of the code base, the chance to create complicated code with tangled dependencies increases. It is hard to truly encapsulate code as every public
class becomes part of your public API and there is no clear notion of explicit dependencies between different parts of the system.
The Jigsaw project that is included in the new Java version is meant to solve all these issues. The modules will consist of usual classes and new module declaration file. This module descriptor explicitly defines what dependencies our module requires and what modules are exposed for outside usage. All packages that are not mentioned in the exports clause
will be encapsulated in the module by default.
A simple module declaration that exports some of its package and requires others can look like this:
1 2 3 4 | module eu.dreamix.java9.modules.accessories { requires eu.dreamix.java9.modules.powersupply; exports eu.dreamix.java9.modules.accessories.headphones; } |
If you want more examples on how you can build application modules or to get familiar with the Jigsaw syntax and project you can visit the quick start guide here.
SEE MORE: Java 9 modules – JPMS basics
2. HTTP/2.0 support
The main difference between HTTP/1.1 and HTTP/2 is how the data is framed and transported between client and server. HTTP/1.1 relies on a request/response cycle. HTTP/2 allows the server to “push” data: it can send back more data than the client requested. This allows it to prioritize and send the data crucial for loading the web page first. Java 9 will have full support for HTTP 2.0 and feature a new HTTP client for Java that will replace HttpURLConnection which only works in blocking mode – one thread per request/response pair. This increases the latency and loading times
of web pages. The HTTP client also provides APIs to deal with HTTP features like streams and server push.
Two example HTTP interactions are shown below, they are taken from the Java 9 documentation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | HttpClient client = HttpClient.newHttpClient(); // GET HttpResponse<String> response = client.send( HttpRequest .newBuilder( new URI( "http://www.foo.com/" ) .headers( "Foo" , "foovalue" , "Bar" , "barvalue" ) .GET() .build(), BodyHandler.asString() ); int statusCode = response.statusCode(); String body = response.body(); // POST HttpResponse<Path> response = client.send( HttpRequest .newBuilder( new URI( "http://www.foo.com/" )) .headers( "Foo" , "foovalue" , "Bar" , "barvalue" ) .POST(BodyProcessor.fromString( "Hello world" )) .build(), BodyHandler.asFile(Paths.get( "/path" )) ); int statusCode = response.statusCode(); Path body = response.body(); // should be "/path" |
3. Improved Javadoc
From my experience working for Dreamix, a Java development company, sometimes the pleasure is in the small things. Currently, if you want to find some class documentation you have to search in google. In Java 9 there are several improvements to the Javadoc and one of them is the addition of a search box.
In Java 8:
In Java 9:
4. Stream improvements
The Stream API was one of the game changing feature in Java 8 and with Java 9 it has gotten even better. Now you will be able to create Stream from Optional. There are also four new methods added to the Stream interface: iterate, dropWhile, takeWhile, ofNullable.
DropWhile discards the first items of the stream until a condition is met.
TakeWhile processes items until a condition is met.
SEE MORE: Java 9 misconceptions & Java 10 wish list: A tell-it-all interview with Java influencers
Iterate allows you to write proper replacements for the for loops using streams. It takes the initial value of the stream, the condition that defines when to stop iterating and the step function to produce the next element.
OfNullable as the name suggest let you create streams from objects without the need to check for nulls. It returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
5. Easier initialization of collections with new factory methods
Currently if you want to create a List of predefined values you have to do a lot of typing:
1 2 3 4 | List<String> euCountries = new ArrayList<>(); euCountries.add( "France" ); euCountries.add( "Bulgaria" ); euCountries.add( "Germany" ); |
In the future, the initialization of common collections will be a lot easier with the newly added factory methods. The static methods in the interfaces make it possible and the List, Set, and Map interfaces are enhanced to have the method for collection creation with up to 10 elements. The resulting objects are immutable collections that are optimized for performance.
Adding items to these collections after creation results in an `UnsupportedOperationException`.
The above code will change to the nice looking:
1 | List.of( "France" , "Bulgaria" , "Germany" ); |
These are some of the added methods:
6. Private methods in interfaces
Java 8 gave us the default methods in interfaces. These methods have body and give behavior to the interfaces, not only empty signatures. What would you do if you have two public methods that do almost the same thing? Most probably you will try to move the common code in a private method and call it from the public ones. But what would you do in a similar situation where you have two default methods in interface instead of two public methods in a class?
In Java 9, you can use the exact same approach and have a private method with the common logic and this method will not be part of your API.
SEE MORE: Java 9 and the future of modularity: Will Project Jigsaw be a hit or flop?
7. Language and syntax improvements
Now it will be easier to write the try with resources statement. Previously all the resources that that have to be closed after the execution had to be initialized in the try clause as in this example:
1 2 3 | try (Resource res = new Resource( "res" )){ //Code using res object } |
From Java 9 we can use final and effectively final resources in the try clause just like that:
1 2 3 4 5 | Resource res1 = new Resource( "res1" ); final Resource res2 = new Resource( "res2" ); try (res1;res2){ //Code using resource objects } |
From Java 9 variable name cannot be consist of a single underscore (“_”). It will be possible to write underscores in your variable names as in my_var, but alone underscore will result in error. The reason behind this that the underscore will be reserved for future use in the language.
We will be able to use diamond operator in conjunction with anonymous inner
classes:
1 2 | BarClass<Integer> bar = new BarClass<>( 1 ) { // anonymous inner class }; |
8. Enhanced process API
So far, the ability to manage and control operating system processes was limited. Furthermore, the code that you wrote to do such interactions was dependent on the OS.
The new release will extend the ability to interact with the operating system. New methods will be added to handle PID management, process names and states, child process management and lot more.
An example code that retrieves current process PID and works on all operating systems will look like this:
1 | System.out.println( "Current PID is " + Process.getCurrentPid()); |
SEE MORE: Java 9 fails and how to fix them
9. Java REPL = Jshell
Last but not least Java9 will include Read Evaluate Print Loop (REPL) tool from the project Kulla( http://openjdk.java.net/projects/kulla/ ). This command line tool is called jshell and if you want to write a few lines of code on their own to test something this will be the perfect tool.
You will not need new class with main method just to execute simple command.
1 2 3 4 5 | jdk- 9 \bin>jshell.exe | Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> 2 + 2 | Expression value is: 4 |
What we will not see in the new release?
There are several good features that got dropped from the upcoming release. However, we will be waiting for them in Java 10.
A standardized and light-weighted JSON API is hyped by many java developers. However, it didn’t make the cut due to funding issues. Mark Reinhold, chief architect of the Java platform, said on the JDK 9 mailing list: “This JEP would be a useful addition to the platform but, in the grand scheme of things, it’s not as important as the other features that Oracle is funding, or considering funding, for JDK 9. We may reconsider this JEP for JDK 10 or a later release. ”
More bad news is that the money and currency API also lacked Oracle’s support and didn’t make it for the release.
Source:-jaxenter.