Java 11 : what’s new ?

Java 11 : what’s new ?

Now that Java 11 is out, it is time to look at the new features that this version brings to us, developers.

This article is part of a series on what’s new on the last versions of Java, for those who wants to read the others, here are the links : Java 10, et Java 9.

Long Term Support

Java 11 is the first Java version that will be Long Term Support – LTS, so with a three years support, until the next LTS that will be Java 17. The rhythm of the LTS is one version each 3 years. Java 8 is still supported until January 2019 for a commercial use and December 2020 for a personal use (or you need to pay Oracle support that is rather affordable : 25$/month/cpu core).

With Java 11, Java 10 will not be supported anymore, this is the new Java support model of Oracle. But good news for us, Oracle JDK and OpenJDK are now identical builds, other vendors are free to deliver Java version with an enhanced supported if they want to …

Moreover, the OpenJDK build is a simple archive, it is more simple to use, so I advise you to use it. It can be found here :  http://jdk.java.net/11

There’s been a lot of noise around the different Java builds and the new support mode, for more information please refer to the response done by community experts : Java is still free.

The JEP

The JEP (Java Enhancement Process) that have been included in Java 11 can be found here : http://openjdk.java.net/projects/jdk/11/ , when you look at them, there is not a lot of new things for the developers, and a lot of technical JEP in which you can found two new Garbage Collectors (Epsilon and ZGC), that adds to the 4 already existing ones (serial, parallel, CMS and G1). I will go back to these new GC later.

Three JEPs are still worth mentioning :

  • JEP 323 : Local-Variable Syntax for Lambda Parameters : allow the use of the var keyword, new since Java 10, in the lambda signature. It is interesting when using visibility modifiers and annotations. You can omit the parameters types of lambas when the compiler can infer them, there is no interest in using var in this case, but when you need to add modifiers and annotations you previously need to add the type of the parameter, you can now use var instead.
  • JEP 321 : HTTP Client (Standard) : the new HTTP client developed in Java 9 is no longer an incubator API but a stable one in it’s own package (java.net.http). It’s implementation has been totally reworked. It support HTTP/2 and offers a new API totally asynchronous.
  • JEP 330 : Launch Single-File Source-Code Programs : after JShell that introduce a shell for Java, here are the JEP 330 that allow to launch a simple Java program (in one file only) without compilation. The purpose is to lower the ceremonial needed to launch a Java program. The java command allows to directly launch a java program (a file with the java extension), it will then compile it on memory and launch it. On Linux, you can use shebang to make a Java file executable using for example : #!/usr/bin/java

The GCs

Two new GCs have been integrated into Java 11:

  • JEP 318 : Epsilon: A No-Op Garbage Collector : this GC has been developed by Aleksey Shipilev in a few hours in the purpose of prototyping a GC that does nothing : it didn’t manage the heap, it just allocate in memory each object at creation time and when there is no more memory, it shutdown the VM! This GC has been designed for experienced people that works in the field of GC.
  • JEP 333 : ZGC: A Scalable Low-Latency Garbage Collector : this GC has been developed by Oracle Labs, it aims to be a new generation of GC that can achieve very high performance with very big heaps (10ms max pause with heaps that can be TB) by sacrificing some memory and CPU. Still experimental it didn’t aims to be the replacement of G1 but target applications that need a lot of memory.

In addition to these two new GC, there as been a lot of optimizations done for G1, that is the GC by default since Java 9. From the author of these changes, by switching to Java 11 from Java 8, we will have 60% lower pauses “for free” with a reduced memory footprint.

More information on these changes here : http://mail.openjdk.java.net/pipermail/hotspot-gc-use/2018-June/002759.html

The changes on the Java API  :

Despite the lack of big API changes in Java 11, and not a lot of JEP for developers, we can find a lot of small changes that will simplified our lives :

  • JDK-8137326 : Method to compare CharSequence, StringBuilder, and StringBuffer :  compareTo().
  • JDK-8196298 : Add a no-op implementation to Reader and Writer : Reader.nullReader() and Writer.nullWriter().
  • JDK-8139206 : InputStream.readNBytes(int len).
  • JDK-8184692 : Pattern.asMatchPredicate : create a predicate which test if the pattern match the String in parameter.
  • JDK-8184693 : Optional.isEmpty() : return true if the value is not present.
  • JDK-8201276 : New method in Files to read/write from/to a String.
  • JDK-8202385 : New annotation java.io.Serial that allow to mark a method relative to the serialization mechanism.
  • JDK-8050818 : Predicate.not() : negate a predicate.
  • JDK-8204375 : TimeUnit.convert(Duration) : conversion from a Duration to a TimeUnit.
  • JDK-8202216 : New methods compareToUnsigned (buffer mismatch) to java.nio.*Buffer.
  • JDK-8201593 : Display the size of the array in the message of the exception ArrayIndexOutOfBoundsException.
  • JDK-8060192 : New method Collection.toArray(). To remove a possible compatibility issue, a default method has been provided.

The String API have a lot of new methods, others are in development for Java 12 and the support of the Raw String :

  • JDK-8197594 : String::repeat : create a String that is the repetition of the ones from which the method has been called.
  • JDK-8198837 : Character::toString(int) : create a String from the character defined by the int (a code point).
  • JDK-8200377 : String::strip, String::stripLeading, String::stripTrailing : create a String by removing white spaces (as defined by  Character.isWhitespace).
  • JDK-8200436 : String::isBlank : return true if the String is empty or only include white spaces (as defined by Character.isWhitespace).
  • JDK-8200380 : String::lines : return a Stream with the lines of the String. More performance than String.split because the split is done lazy.

To go further, an article almost exhaustive of all the changes  : https://www.azul.com/90-new-features-and-apis-in-jdk-11/ 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.