Java 11

Java 11, released in September 2018, brought several improvements and new features to the Java platform. Some of the notable features introduced in Java 11 include:

Local-Variable Syntax for Lambda Parameters:

This feature extends the use of the var keyword introduced in Java 10 to lambda parameters. It allows you to use var when declaring the types of lambda parameters, enhancing code readability.

  List<String> list = Arrays.asList("a", "b", "c");
  String result = list.stream()
          .map((var x) -> x.toUpperCase())
          .collect(Collectors.joining(","));
  System.out.println(result2);
import org.jetbrains.annotations.NotNull;

  List<String> list = Arrays.asList("a", "b", "c", null);
  String result = list.stream()
          .map((@NotNull var x) -> x.toUpperCase())
          .collect(Collectors.joining(","));
  System.out.println(result3);

HTTP Client (Standard):

Java 11 introduced a new HTTP client as part of the standard library (java.net.http) to support HTTP/1.1 and HTTP/2. This client provides a more modern, flexible, and efficient alternative to the HttpURLConnection API.

    HttpClient httpClient = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_1_1)
            .connectTimeout(Duration.ofSeconds(10))
            .build();

    HttpRequest request = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create("https://httpbin.org/get"))
            .setHeader("User-Agent", "Java 11 HttpClient Bot")
            .build();

    HttpResponse<String> response =
      httpClient.send(request, HttpResponse.BodyHandlers.ofString());

    HttpHeaders headers = response.headers();
    headers.map().forEach((k, v) -> System.out.println(k + ":" + v));

    System.out.println(response.statusCode());
    System.out.println(response.body());

Nest-Based Access Control:

Nestmates are classes which are logically part of the same code entity, such as inner classes. Java 11 introduced nest-based access control, which allows private access between nestmate classes without the need for compilers to insert synthetic bridge methods.

Epsilon Garbage Collector:

Java 11 introduced the Epsilon garbage collector, a no-op garbage collector designed for performance testing, reducing memory footprint, or scenarios where garbage collection isn’t required. Once the Java heap is exhausted, the JVM will shut down. A few use cases:

-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC

ZGC (Experimental):

Java 11 introduced Z Garbage Collector (ZGC) as an experimental feature. ZGC is a low-latency garbage collector designed to handle very large heaps with pause times not exceeding 10 milliseconds.

Flight Recorder (Commercial Feature):

Java Flight Recorder (JFR), previously a commercial feature, was open-sourced and included in Java 11. JFR is a profiling and diagnostics tool that collects data about the JVM and the applications running on it with low overhead.

$ java -XX:StartFlightRecording=duration=60s,settings=profile,filename=app.jfr MyHelloWorldApp

Deprecation of Java EE and CORBA Modules:

With Java 11, several modules related to Java EE and CORBA (e.g., java.xml.ws, java.xml.bind, java.corba) were marked as deprecated, as part of the modularization efforts and the transition from Java EE to Jakarta EE.

TLS 1.3 Support:

Java 11 added support for TLS 1.3, the latest version of the Transport Layer Security protocol, offering improved security and performance over previous versions.

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

SSLSocketFactory factory =
        (SSLSocketFactory) SSLSocketFactory.getDefault();
socket =
        (SSLSocket) factory.createSocket("google.com", 443);

socket.setEnabledProtocols(new String[]{"TLSv1.3"});
socket.setEnabledCipherSuites(new String[]{"TLS_AES_128_GCM_SHA256"});

These features, along with various performance enhancements and bug fixes, contributed to making Java 11 a significant release in the evolution of the Java platform.