Java 11, the Good Parts
Tags: java
Published: 2020-02-25 14:45:40

Announced in 25 September 2018, Java 11 is the latest long term support release by Oracle for the Java community. The following is a listing of the most important features and changes:

## Type inference for Lambda parameters

The `var` key word was introduced in Java 10 to implement local scope variables with dynamic type inference.

```java
var aString = "This is a String";
```

In Java 11, the `var` key word can be used to qualify a given lambda parameter

```java
Predicate<String> notNull  = (var u) -> u != null;
```

## HTTP Client

This is known to be an incubating feature from JDK 9. It's a standard API to support HTTP/1, HTTP/2 clients, synchronous, asynchronous and in a reactive-functional style. This being illustrated by the following code from the Oracle HTTP Client Page:

````java
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("http://openjdk.java.net/"))
      .build();
client.sendAsync(request, asString())
      .thenApply(HttpResponse::body)
      .thenAccept(System.out::println)
      .join();
```

## TLS 1.3

Java 11 comes with the implementation of the Transport Layer Security Protocol (TLS) version 1.3. TLS is protocol for providing secured data exchange between two ends of a communication channel. It defines a secure communication channel as having those 3 main criteria:

- Authentication
- Confidentiality
- Integrity

TLS 1.3 consists of two main specifications:

- Communication establishment, aka Handshake phase management, where the two peers negotiates the security of the exchange via key exchange and cryptographic algorithms
- Data Exchange phase, aka Record Protocol where the traffic is considered as a transit of records protected by the parameters from the first phase.

Enough as a digression, please see the link in the References paragraph for more. The JDK steering committee included the TLS 1.3 implementation because of its robustness as a security protocol and to keep competitive with other vendors implementing it. Following suit, the TLS implementation includes various cryptographic algorithms for signature and encryption.

## Performance related features

JDK 11 put a big deal on performance enhancement and tooling. Aware of the fact that Garbage Collection is the pivotal point for Java performance, the following features were introduced for GC:

- **`ZGC`**: although experimental, the ZGC promises low latency time (less than 10ms) and wide range of memory size support (from hundred of megabytes to terabytes). It's only available for Linux x64.
- **`Epsilon GC`**: a generous GC that allocates memory without reclaiming it. Useful for performance testing and short living Java processes.

Performance tools have a lion share in this release:

- **`Low Overhead Heap Profiling`**: offers a non intrusive way to monitor Java memory heap allocation. It's in fact a programmatic native API available for the JVMTI (JVM Tooling Interface) to get information about object on the Heap.
- **`Java Flight Recorder`**: an important tool for low overhead performance data troubleshooting and collection. It produces dump data that can be red by Java Mission Control tool. In a nutshell, we start the tool on the command line as the following

```shell
jcmd <pid> JFR.start
```

where `pid` is the process id of an already running java application. To specify the dump process file that will record the application's performance status use the command

```shell
jcmd <pid> JFR.dump filename=recording.jfr
```

To stop the recording, issue the command

```shell
jcmd <pid> JFR.stop
```

and the produced file can be imported by other profiling tools.

## Deprecation and removal of some features from the JDK

JavaFx is no more a part of the JDK, it's a separate download. Oracle stopped providing 32bit binaries and auto updates via Server JRE.
The following modules are removed:

- Corba
- JAXB, JAX-WS, JAF and Common Annotations
- Nashorn JavaScript Engine is being deprecated
- The packing suit of Java WebStart and Java Plugin

For Corba and the other J2EE modules (like JAXB and JAX-WS) the removal was justified by the fact that they can evolve and be maintained outside the JDK sphere and it becomes more and more difficult to sync the changes.

## Minor Features

With JDK 11 comes the support for Unicode 10 which means we can print emojies. Another feature is the possibility to execute single java file programs in one pass by calling

```shell
java OneClassFileProgram.java
```

instead of the usual sequence (javac then java). Under the hood, no class file on the disc, all in memory.

## References

- Main feature page: http://openjdk.java.net/projects/jdk/11
- Oracle migration guide: https://docs.oracle.com/en/java/javase/11/migrate/index.html
- Oracle HTTP Client: http://openjdk.java.net/groups/net/httpclient/recipes-incubating.html
- TLS 1.3: https://tools.ietf.org/html/rfc8446