logo le blog invivoo blanc

Java 11 : 9 new things to discover

6 August 2019 | Java | 0 comments

Java 11

Java 11, released in september 2018, is a major novelty of the Java language in that it is an LTS version (Long Time support) that will be supported by Oracle until 2023 – and beyond if we want to benefit from extended support. As a result, Java 11 appears as an unavoidable version on the horizon of the coming years. In this article, you have the opportunity to familiarize yourself with a selection of 9 new features contained in the JDK 11.

1. Type inference for the arguments of Lambda expressions

This evolution is a continuation of the variable type inference added with Java 10. It is now possible not to explicitly specify the type of lambda parameters simply by using the var keyword.

For example,

will become:

This can represent both a gain in clarity, brevity and time when using certain types which are heavy to write. Of course, it was already not mandatory to specify a type for lambda parameters so the declaration:

is also correct.

What is the purpose of type inference in this case? It is useful if we want to annotate our parameters, which is impossible without a type declaration. In java 11, we can for example write expressions such as:

2. Simplified launch of single file programs

Before Java 11, to launch the traditional HelloWorld.java, we executed:

From now on, with Java 11, one can directly launch applications contained in a single fila via:

This feature, in addition to JShell, complements the tools available to novice developers and also allows Oracle to upgrade Java to a lighter running language. his may illustrate its willingness to bring Java into the scripting language course in the future. Let’s wait and see!

3. Evolution of the http client api

For users of the HTTP Client API introduced by Java 9 and updated by Java 10, be aware that its implementation has been almost entirely revised by Java 11. Oracle ensures that these revisions do not affect the API from a high-level standpoint, while improving a number of technical points as a result of community feedback.

The implementation is now completely asynchronous. The data flow can now be easily tracked, from user requests to sockets. This should greatly reduce the complexity of the code and maximize the possibility to reuse HTTP / 1.1 and HTTP / 2.

The name of the API module and package is now java.net.http.

4. APis improvements

JDK 11 embeds a number of new classes and methods built into already existing modules. The list below is a non-exhaustive overview, highlighting the additions that seem appear to be, in my opinion the most important.

  • java.lang.String:
    • boolean isBlank (): Returns true if the String is empty or composed only of whitespaces, otherwise false.
    • stream lines (): Returns a stream of lines extracted from the String.
    • string repeat (int): returns a String that is the original String concatenated with itself n times.
    • String strip (): returns a String free of trailing and leading whitespaces. To put it simply, strip () is the “Unicode-aware” version of trim () whose definition of whitespace dates back from the first versions of Java.
    • String stripLeading (): Returns a String free of its leading whitespaces.
    • String stripTrailing (): returns a String free of trailing whitespaces.
  • java.util.function.Predicate
    • Predicate not (Predicate). Returns a Predicate which is the negation of the Predicate passed as an argument. Example to filter a list:
  • java.lang.CharSequence
    • int compare (CharSequence, CharSequence): compares two instances of CharSequence in lexicographic order. Returns a negative, null or positive value.
  • java.lang.StringBuffer / java.lang.StrinBuilder
    • Both classes now have access to a new compareTo () method that takes a StringBuffer / StringBuilder argument and returns an int. The comparison logic follows the same lexicographic order as for the new method of the CharSequence class.

5. Visibility management of nested classes attributes

Java allows multiple classes to be declared in a single source file, such as nested classes. From the user’s point of view, however, they are generally considered to belong to the “same class”. And, therefore, users expect them to share a common access regime relative to their attributes.

To preserve these expectations, compilers must extend private attribute access to classes in the same package by adding access bridges. An invocation of a private member is compiled into a call of a compiler-generated method (getter) in the target class, which in turn accesses the intended private member.

For example, in the case of a NestedClass class, nested inside a NestingClass class, which needs to access one of the private attributes of the host class:

The compiler separates the two classes and creates a public access method to nestingInt used bye the NestedClass class:

These bridges subvert the encapsulation (private no longer has the exact same meaning) and can confuse users and tools. A formal notion of a group of nesting class files (or nest), where nest partners share a common access control mechanism, makes it possible to directly obtain the desired result in a simpler, more secure, and more transparent way.

To easily connect nested classes and hosts in JDK 11, two new attributes have been added to the classes: NestHost (new host) and NestMembers (nest members).

3 methods are also added to jave.lang.Class:

  • Class getNestHost ()
  • Class [] getNestMembers ()
  • boolean isNestmateof (Class)

6. Dynamic class-file constants

This change is an extension of the class file format to support a new constants pool: CONSTANT_Dynamic.

It seems contradictory for a value to be both constant and dynamic. However, you can essentially consider it as a final value in Java. The value of the constant is not defined at compile time (unlike other constants), but uses a bootstrap method to determine the value at run time. The value is therefore dynamic, but since its value is only defined once, it is also constant. This feature introduces a new class, java.lang.invoke.ConstantBootstraps, with nine new methods.

7. The epsilon garbage collector

The Epsilon garbage collector is a little unusual in the sense that it does not collect waste! It will allocate memory, as needed, when instantiating new objects but will not recover any space occupied by unreferenced objects. This is a totally passive, openly experimental, “no-op” GC implementation. It is an isolated change, does not affect other GCs an makes minimal changes to the rest of the JVM. This GC is mainky useful when doing performance or memory allocation tests as wall as tasks with a very short lifecycle.

8. Unicode 10

The JDK 11 has taken note of the arrival of Unicode 10, and therefore supports the new version of this standard. This version includes 8,518 new symbols (from Bitcoin to emojis through traditional Chinese, Arabic or Buddhist signs) that you can use with Java 11.

9.Deleted modules

The Java EE and CORBA modules, after being deprecated in JAVA SE 9, were removed from JAVA SE Platform and JDK 11. The impacted modules are:

  • corba
  • transaction
  • activation
  • xml.bind
  • xml.ws
  • xml.ws.annotation

JavaFX modules were also removed from JDK 11. These modules were part of previous Oracle JDKs, but absent from the latest OpenJDKs. However, do not panic! These modules are always accessible outside of the JDK.

Bonus: On the influence of age and Japanese imperial tradition on the java.time.chroni API.

Surprisingly, politics has ramifications even in the most sacred realms of all, including the Java language! In making the decision Making the decision, for health related reasons, to give up his position as Emperor of Japan to his son Naruhito, the current Emperor Akihito will put an end to the Heisei era on April 30th, 2019. A new era will begin, full of promise and hope, but also of developers’ headaches, since the name of the future era is not yet known. Thus, any information system integrating the Japanese calendar must not only be ready to digest the change of era, but also provide a mechanism for updating its name when it is known.

Fortunately, Oracle has thought of everything, since the java.time.chrono and java.util packages of JDK 11 will support this change of era. Today, a generic name has been assigned to it (“元 号” in Japanese, “NewEra” in other languages) and it will be replaced by the real name in an upcoming update. It is therefore strongly advised not to create dependencies on this placeholder.

I convey my encouragement to those who suffer the daily impacts of these geopolitico-calendrical changes, making their work so much more painful. This is revealed by the gymnastics needed to recover the name of a Japanese era, a process of exemplary simplicity (here the expected return in Java 11 is the String “NewEra”):

or even more limpid:

We wish you a pleasant jurney with Java 11! You may need to enjoy it quickly because Java 12 is coming very soon … staring march 2019!

Additional links on Java 11 :