September 15, 2017
9 Big Things about Java 9
According to the latest Tiobe index of programming language popularity, Java tops the list. No wonder there is so much buzz around the new Java 9 release, set for September 21, 2017. Take a walk through with our expert.
Many compiled languages include tools for statements interpretation. By using these REPL tools you can rapidly test code snippets without creating a project.
Take Scala, as an example. Compilation can be time consuming, but by using REPL, each statement is executed instantly! That’s great when you are getting started with the language. Each expression returns a value and type, which is valuable information to have.
On the other hand, in Java, we would need to create a test or main method that prints results and must be recompiled every time someone makes a change.
2. COLLECTIONS API FACTORY METHODS
The added static factory methods help create collections in variety of small cases.
This come to life due to static interface methods. Thus collections are immutable.
3. COMPACT STRINGS
Java 9 provides new and improved strings which, in most cases, will reduce String memory consumption by half.
Instead of having a char array, String is now represented as a byte array. Depending on which characters it contains, it will use either UTF-16 or Latin-1 to produce either one or two bytes per character.
There is a also new field inside the String class–coder that indicates which variant is used. Unlike Compressed Strings, this feature is enabled by default. If necessary, as in a case where mainly UTF-16 Strings are used, it can be disabled by -XX:-CompactStrings.
At last, Java9 adds an or() method to Optional API, which allows for the return of different chained optionals without calling isPresent() each time.
In addition, Java9 can create Stream out of Optional with no more than 1 element inside, which is a real advantage when «lazy» mapping is needed.
And, as the cherry on the cake, Java9 will have an ifPresentOrElse() method in Optional API.
5. COMPLETABLE FUTURE
This class was polished and several methods were added, but most notable is copy(). This method returns immutable copy for CompletableFuture.
But the coolest thing is that stopping an ancestor will affect the child!
Timeouts were also added to the API. Now it is possible to define how CF should finish after time runs out.
With the excellent additions of dropWhile() and takeWhile() to Stream API, there is now option to skip loop and use the stream iterate() method instead.
7. PROCESS HANDLE API
When starting Java9, there is now a straight and simple access to process handling.8. JAVA LANGUAGE ENHANCEMENT
Just in case if someone still uses it, «_» is no longer a legitimate identifier. In this case you should use « __» instead (but you don’t 😉 )
Private methods inside Interfaces.
Now with Java9 we are able to use final variables inside try-with-resources effectively.
9. STACK WALKER AND G1 AS DEFAULT GC
We can now use stack-trace without creating an Exception instance. We consider this a big improvement.
Starting Java9 G1 will be our default GC. The “Garbage-first” garbage collector, aka G1, is a concurrent multi-threaded GC. It mostly works alongside the application threads much like the concurrent mark sweep GC, and is designed to offer shorter, more predictable pause times while still achieving high throughput. What makes G1 different is that instead of splitting the heap into 3 big regions, it splits it into a set of lots of equal-size. Certain subsets of regions are still assigned roles just like in the other GCs. The amount of live data in each region is tracked, and when a collection is triggered, the G1GC will clear the ones with the most “garbage” first, hence the name. By doing this, it attempts to free as much space as possible with each collection.