Overview of New Features in Java 11

Introduction Java 11 was released on 25th September 2018, six months after Java 10Click to read: Java 10. It was the first long-term support (LTS) release under Java's six-month release cadence - Java 9 and Java 10 were supported only until the next release, while Java 11 receives updates for years. Many applications therefore moved straight from Java 8 to Java 11. This article gives an overview of the new features in Java 11. It goes through all 17 JDK Enhancement Proposals (JEPs) in the release, grouped into language and library features, JVM and performance features, security features, and removals and deprecations, and links to a detailed tutorial for each major feature. Language and library features
  1. HTTP Client (JEP 321) - A modern HTTP client in the java.net.http package, which supports HTTP/2, WebSocket, and both synchronous and asynchronous requests. It had been an incubator module in Java 9 and 10, and replaces the old HttpURLConnection. Read the Java 11 HTTP Client API tutorialClick to read tutorial: Java 11 HTTP Client API Tutorial with Examples.
  2. Local-Variable Syntax for Lambda Parameters (JEP 323) - var can now be used for lambda parameters, as in (var s, var n) -> s.repeat(n), which allows annotations and final on them. Read about var in lambda parametersClick to read tutorial: Java 11 var in Lambda Parameters with Examples.
  3. Launch Single-File Source-Code Programs (JEP 330) - java Hello.java compiles a source file in memory and runs it, without a separate javac step. It also allows Java programs to be written as shebang scripts. Read how to run single-file source code programsClick to read tutorial: Java 11: Run Single-File Source Code Programs with java.
  4. New String methods - isBlank(), lines(), strip(), stripLeading(), stripTrailing() and repeat(). Read about the new String methods in Java 11Click to read tutorial: Java 11 String Methods: isBlank, lines, strip, repeat.
  5. Other API additions - Files.readString() and Files.writeString(), Path.of(), Predicate.not(), Optional.isEmpty(), Collection.toArray(IntFunction) and more. These are small API changes, so they have no JEPs of their own. Read about the Java 11 API additionsClick to read tutorial: Java 11 API Additions: Files.readString, Predicate.not.
  6. Nest-Based Access Control (JEP 181) - Explained in detail below.
  7. Dynamic Class-File Constants (JEP 309) - A new constant-pool form in class files, CONSTANT_Dynamic, whose value is computed by a bootstrap method the first time it is used, similar to the way invokedynamic works for method calls. It is used by language and compiler writers rather than in Java source code.
  8. Unicode 10 (JEP 327) - String, Character and the other text APIs were upgraded from Unicode 8.0 to Unicode 10.0, adding over 16,000 characters, including new emoji, and 10 new scripts.
Nest-based access control Java has always allowed a nested class and its outer class to access each other's private members. The JVM, however, had no such concept - it saw them as two separate classes. To make the access work, the compiler generated hidden package-private bridge methods (named like access$000), which widened the access and added an extra method call. Java 11 adds nests to the JVM: classes which are compiled from the same source file and top-level class are nestmates, and the JVM lets nestmates access each other's private members directly. This removes the bridge methods, and makes reflection consistent with the language. Java 11 also added methods to Class to query nests -
Querying nests with the new Class methods
//NestmatesDemo.java
import java.lang.reflect.Method;
import java.util.Arrays;

public class NestmatesDemo {
  private String secret() {
    return "outer secret";
  }

  class Inner {
    String peek() throws Exception {
      // Reflective access to a private method of a nestmate - allowed from Java 11
      Method m = NestmatesDemo.class.getDeclaredMethod("secret");
      return (String) m.invoke(NestmatesDemo.this);
    }
  }

  static class Helper { }

  public static void main(String[] args) throws Exception {
    System.out.println("Nest host of Inner: " + Inner.class.getNestHost().getSimpleName());
    System.out.println("Inner is nestmate of Helper: " + Inner.class.isNestmateOf(Helper.class));
    System.out.println("Nest members: " + Arrays.toString(
        Arrays.stream(NestmatesDemo.class.getNestMembers()).map(Class::getSimpleName).toArray()));
    System.out.println("Inner read: " + new NestmatesDemo().new Inner().peek());
  }
}
 OUTPUT of the above code
Nest host of Inner: NestmatesDemo
Inner is nestmate of Helper: true
Nest members: [NestmatesDemo, Helper, Inner]
Inner read: outer secret
  • getNestHost() returns the top-level class, which is the nest host. getNestMembers() returns the host and all the classes nested in it, and isNestmateOf() checks whether two classes are in the same nest.
  • Inner calls the private method secret() of NestmatesDemo through reflection without calling setAccessible(true). Before Java 11 this threw an IllegalAccessException, even though the same call written directly in Java code compiled and ran fine.
JVM, garbage collection and monitoring
  1. ZGC: A Scalable Low-Latency Garbage Collector (JEP 333) - An experimental garbage collector designed to keep pause times below 10 milliseconds, even for heaps of many terabytes, by doing almost all of its work while the application keeps running. In Java 11 it was available only on Linux/x64, and enabled with -XX:+UnlockExperimentalVMOptions -XX:+UseZGC. ZGC became a production feature in Java 15.
  2. Epsilon: A No-Op Garbage Collector (JEP 318) - An experimental garbage collector which allocates memory but never frees it. When the heap is full, the JVM exits. It is meant for performance testing, to measure an application without any GC overhead, and for very short-lived programs. It is enabled with -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC.
  3. Flight Recorder (JEP 328) - Java Flight Recorder (JFR), a low-overhead tool for collecting diagnostic and profiling data from a running JVM, was open-sourced and added to OpenJDK. Before Java 11 it was a commercial feature of Oracle JDK. A recording can be started with -XX:StartFlightRecording or with jcmd, and analysed with JDK Mission Control.
  4. Low-Overhead Heap Profiling (JEP 331) - A way for profiling tools to sample Java heap allocations through the JVM Tool Interface (JVMTI), with low enough overhead to be left on in production.
  5. Improve Aarch64 Intrinsics (JEP 315) - Faster implementations of string, array and math functions, such as Math.sin() and Math.log(), on 64-bit ARM processors.
Security
  1. Transport Layer Security (TLS) 1.3 (JEP 332) - Support for TLS 1.3, the latest version of the protocol behind HTTPS, which is faster to connect and removes older insecure algorithms. It is used by default when both sides support it.
  2. ChaCha20 and Poly1305 Cryptographic Algorithms (JEP 329) - The ChaCha20 stream cipher and the ChaCha20-Poly1305 authenticated cipher, available through the standard Cipher API as "ChaCha20" and "ChaCha20-Poly1305". They are fast in software, without special hardware support.
  3. Key Agreement with Curve25519 and Curve448 (JEP 324) - Key agreement using the X25519 and X448 elliptic curves, which are more efficient and easier to use securely than the older curves, available through KeyAgreement.getInstance("X25519").
Removals and deprecations
  1. Remove the Java EE and CORBA Modules (JEP 320) - JAXB, JAX-WS, JAF, Common Annotations, JTA and CORBA were removed from the JDK, along with tools such as xjc and wsimport. Applications which use them must now add them as libraries. Read about the removed modules and how to migrateClick to read tutorial: Java 11 Removed Modules: Java EE, CORBA and JavaFX.
  2. Deprecate the Nashorn JavaScript Engine (JEP 335) - The Nashorn engine was deprecated for removal. It was removed in Java 15.
  3. Deprecate the Pack200 Tools and API (JEP 336) - The Pack200 JAR compression format was deprecated for removal. It was removed in Java 14.
Outside the JEPs, Oracle also stopped bundling JavaFX, Java Web Start and the browser plugin for applets with its JDK from Java 11. JavaFX continues as the separate OpenJFX project. Oracle JDK, OpenJDK and the LTS release From Java 11, Oracle JDK and the OpenJDK builds are built from the same code and are functionally the same - features such as Flight Recorder, which used to be exclusive to Oracle JDK, are now in OpenJDK. At the same time Oracle changed the license of Oracle JDK, so that using it in production needs a commercial subscription. As a result, many teams now use free OpenJDK builds of Java 11, such as Eclipse Temurin, Amazon Corretto, Azul Zulu or Microsoft Build of OpenJDK, which provide long-term updates. To set one up, read how to install the JDK and set JAVA_HOMEClick to read: how to install the JDK and set JAVA_HOME. Note that from Java 11 Oracle no longer offers a separate JRE download, since a runtime for an application can be built with jlink; some other vendors still provide JRE builds. Summary In this article we went through the new features of Java 11, the first LTS release under the six-month release cadence. On the language and library side, Java 11 brought the standard HTTP Client, var in lambda parameters, running single-file programs with the java command, and new methods in String, Files, Optional and Predicate. We saw how nest-based access control removed the compiler's bridge methods for private access between nested classes. In the JVM, Java 11 added the experimental ZGC and Epsilon garbage collectors and open-sourced Flight Recorder, and on the security side it added TLS 1.3 and new cryptographic algorithms. Lastly, it removed the Java EE and CORBA modules, and deprecated Nashorn and Pack200. Start with the first detailed tutorial in the series, on the new String methods in Java 11Click to read tutorial: Java 11 String Methods: isBlank, lines, strip, repeat.