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
Nest-based access control
Java has always allowed a nested class and its outer class to access each other's
OUTPUT of the above code
Security
- HTTP Client (JEP 321) - A modern HTTP client in the
java.net.httppackage, 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 oldHttpURLConnection. Read the Java 11 HTTP Client API tutorialClick to read tutorial: Java 11 HTTP Client API Tutorial with Examples. - Local-Variable Syntax for Lambda Parameters (JEP 323) -
varcan now be used for lambda parameters, as in(var s, var n) -> s.repeat(n), which allows annotations andfinalon them. Read about var in lambda parametersClick to read tutorial: Java 11 var in Lambda Parameters with Examples. - Launch Single-File Source-Code Programs (JEP 330) -
java Hello.javacompiles a source file in memory and runs it, without a separatejavacstep. 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. - New String methods -
isBlank(),lines(),strip(),stripLeading(),stripTrailing()andrepeat(). Read about the new String methods in Java 11Click to read tutorial: Java 11 String Methods: isBlank, lines, strip, repeat. - Other API additions -
Files.readString()andFiles.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. - Nest-Based Access Control (JEP 181) - Explained in detail below.
- 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 wayinvokedynamicworks for method calls. It is used by language and compiler writers rather than in Java source code. - Unicode 10 (JEP 327) -
String,Characterand 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.
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());
}
}
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, andisNestmateOf()checks whether two classes are in the same nest.Innercalls the private methodsecret()ofNestmatesDemothrough reflection without callingsetAccessible(true). Before Java 11 this threw anIllegalAccessException, even though the same call written directly in Java code compiled and ran fine.
- 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. - 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. - 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:StartFlightRecordingor withjcmd, and analysed with JDK Mission Control. - 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.
- Improve Aarch64 Intrinsics (JEP 315) - Faster implementations of string, array and math functions, such as
Math.sin()andMath.log(), on 64-bit ARM processors.
- 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.
- ChaCha20 and Poly1305 Cryptographic Algorithms (JEP 329) - The ChaCha20 stream cipher and the ChaCha20-Poly1305 authenticated cipher, available through the standard
CipherAPI as"ChaCha20"and"ChaCha20-Poly1305". They are fast in software, without special hardware support. - 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").
- 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
xjcandwsimport. 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. - Deprecate the Nashorn JavaScript Engine (JEP 335) - The Nashorn engine was deprecated for removal. It was removed in Java 15.
- Deprecate the Pack200 Tools and API (JEP 336) - The Pack200 JAR compression format was deprecated for removal. It was removed in Java 14.
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.Continue Reading Java 11 Tutorials
Java 11 String Methods: isBlank, lines, strip, repeatClick to read tutorial: Java 11 String Methods: isBlank, lines, strip, repeat Java 11 var in Lambda Parameters with ExamplesClick to read tutorial: Java 11 var in Lambda Parameters with Examples Java 11: Run Single-File Source Code Programs with javaClick to read tutorial: Java 11: Run Single-File Source Code Programs with java Java 11 API Additions: Files.readString, Predicate.notClick to read tutorial: Java 11 API Additions: Files.readString, Predicate.not Java 11 HTTP Client API Tutorial with ExamplesClick to read tutorial: Java 11 HTTP Client API Tutorial with Examples Java 11 Removed Modules: Java EE, CORBA and JavaFXClick to read tutorial: Java 11 Removed Modules: Java EE, CORBA and JavaFX
Java 11 String Methods: isBlank, lines, strip, repeatClick to read tutorial: Java 11 String Methods: isBlank, lines, strip, repeat Java 11 var in Lambda Parameters with ExamplesClick to read tutorial: Java 11 var in Lambda Parameters with Examples Java 11: Run Single-File Source Code Programs with javaClick to read tutorial: Java 11: Run Single-File Source Code Programs with java Java 11 API Additions: Files.readString, Predicate.notClick to read tutorial: Java 11 API Additions: Files.readString, Predicate.not Java 11 HTTP Client API Tutorial with ExamplesClick to read tutorial: Java 11 HTTP Client API Tutorial with Examples Java 11 Removed Modules: Java EE, CORBA and JavaFXClick to read tutorial: Java 11 Removed Modules: Java EE, CORBA and JavaFX