Java 9 : Brave New World3 min read
Java is one of the most popular programming languages. It is an object-oriented, cross-platform language, which has gained favor with many developers due to its simplicity and power.
On September 21, 2017, Oracle presented a new JDK 9.0.1 release, in which many innovations were introduced. Let us have a look at the most important of them.
Divide and conquer
Modules in Java 9 are an important change in the structure of the language. Before the advent of Java 9, we used a monolithic JAR to develop Java applications. This approach had enough shortcomings and limitations that are now effectively eliminated with modularity.
It allows to split the program code into parts and organize it into modules depending on the tasks they perform. The modules can be reused if necessary.
It is also worth noting that the Java Platform scalability has now significantly increased, and that is why it is a big step towards Internet of Things (IoT).
JShell: Finally, an Official Shell in Java 9
With the release of Java 9, Oracle presented JShell. This great tool eliminates the need to create a new project each time in order to test certain code structures.
JShell is equipped with history and autofill features, and the ability to save and download written expressions. By the way, when using JShell, one can forget about a semicolon (;). So happiness is in small things 🙂
Collection API Factory Methods
Starting with Java 9, creation of immutable collections has become much easier. Now we have a set of static factory methods available out of the box to create an immutable List, Set, Map and Map.Entry. Let us explore an example:
Using the static factory method List of (E … elements) we create an Immutable List. Once created, we cannot add, modify, or delete items of the collection. As soon as we try to do so, we get UnsupporrtedOperationException:
Stream API Improvements
Speaking of the Stream API, it is worth mentioning new methods. Particular attention should be paid to two of them: dropWhile() and takeWhile(). The dropWhile() method rejects all the elements before the predicate condition is satisfied and returns the elements found after we meet the condition.
The takeWhile() method works the opposite way round: it returns all the elements before the predicate condition is satisfied and rejects all the following. Let us use an example to illustrate the point:
Oracle has not left aside Optional as well, and added one useful method. This is the or() method, which eliminates the need to use isPresent() in some cases. Let us see how it works:
Also, in addition, the ifPresentOrElse() method was added, which combines ifPresent(), isPresent() and OrElse() methods.
If a value exists, the first action is executed, and if not, the second.
Up to this point, ParallelGC has been the default garbage collector. However, it had drawbacks. The larger the heap, the more time it takes to clear the garbage, which affects the overall performance. That is why Java 9 has Garbage First (G1) installed as the GC by default.
The main goal of G1 is to improve performance and reduce the number of pauses. Garbage First Garbage Collector (G1GC) was designed for heaps exceeding 4GB.
Last but not the least
It is also important to note some smaller, but equally important updates. They are:
- Private methods in interfaces;
- New HTTP-client supporting HTTP/2 and WebSocket;
- New cool methods in CompletableFuture.
Software EngineerOur services