Ukraine needs your help!

Support
Series: What is new in Java?

What is new in Java 11?

Java

Java 11 is a TLS version that was released for production use. In this article, I want to review the new features that Java 11 provides for us.

This post was published later, because of building the series “What is new in Java?” of releases of Java starting from Java 11.

JEP 181: Nest-Based Access Control

Resolved an issue of access to inner classes’ fields. using reflection. Simply talking, when we are trying to set a new value to Field received by reflection we have got an IllegalAccessException, as shown below:

import java.lang.reflect.Field;

public class JEP181Example {

  public static class NestedClass1 {
    public void someFunction() throws Exception {
      final NestedClass2 nestedClass2 = new NestedClass2();
      nestedClass2.nestedVariable = 2; // works
      
      final Field field = NestedClass2.class.getDeclaredField("nestedVariable");
      field.setInt(nestedClass2, 2); // throws IllegalAccessException
    }
  }

  public static class NestedClass2 {
    private int nestedVariable;
  }

  public static void main(String[] args) throws Exception {
    new NestedClass1().someFunction();
  }
}

In JEP 181 that issue was resolved and now when I am trying to define a new value of a private variable I don’t get this exception.

JEP 309: Dynamic Class-File Constants

A new record of constant pool CONSTANT_Dynamic will be added to class files, where the creation of that records will be delegated to the method of initial startup, for instance, invokedynamic calls in the execution time.

This feature allows developers to improve the performance of the compilers they develop.

JEP 315: Improve Aarch64 Intrinsics

Improved build-in math functions, such Math.cos() and Math.sin(), which are using for AArch64 (ARM) architecture.

JEP 318: Epsilon. A No-Op Garbage Collector

Added garbage collector which allows more flexible performance testing and/or tracking of the performance of extremely short-lived tasks.

Epsilon - garbage collector which doing nothing.

In fact, it is not a garbage collector, it does not collect the garbage. And added for debugging and testing purposes only.

JEP 320: Remove the Java EE and CORBA Modules

Deprecated modules in Java 9 such as Java EE and COBRA have been deleted. Based on that, Java 11 was deleted the following packages:

  • java.xml.ws;
  • java.xml.bind;
  • java.activation;
  • java.xml.ws.annotation;
  • java.corba;
  • java.transaction;
  • java.se.ee;
  • jdk.xml.ws;
  • jdk.xml.bind.

Also, was deleted CosNamingProvider. And Java RMI would not provide IDL and IIOP stubs.

JEP 321: HTTP Client (Standard)

HTTP Client which was a part of an incubator in the scope of Java 9 and Java 10, but now is a standard module in Java 11 java.net.http. API supports HTTP 1.1, HTTP 2, and also WebSocket.

public class JEP321Example {
  public static void main(String[] args) throws Exception {
    final HttpClient client = HttpClient.newBuilder()
        .version(Version.HTTP_2)
        .build();
    
    final HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://bcode.dev"))
        .build();

    // Synchronous Call
    final HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
    System.out.println(response.statusCode())

    // Asynchronous Call
    client.sendAsync(request, BodyHandlers.ofString())
        .thenApply(HttpResponse::statusCode)
        .thenAccept(System.out::println)
        .get();
  }
}

JEP 323: Local-Variable Syntax for Lambda Parameters

In Java 10 has been included a new keyword var, for local variable declarations.

var numer = 10;

But we couldn't use it in lambda expressions. JEP 323 fixed it and allows us to use var in lambda expressions.

var numbers = new int[] {11, 2, 3, 14, 5, 23};
int sum = Arrays.stream(numbers)
            .filter((var number) -> number < 10)
            .sum();

JEP 324: Key Agreement with Curve25519 and Curve448

RFC 7748 key agreement with using Curve25519 and Curve448 implemented in SunEC provider.

JEP 327: Unicode 10

Java 11 supports Unicode 10. Such as Java 10 supported Unicode 8 based on that will be added changes to Unicode 9 and Unicode 10.

But sorting algorithm of Unicode and mechanics of Unicode security and compatibility processing of Unicode IDNA and Unicode Emoji will not be implemented.

JEP 328: Flight Recorder

Flight Recorder - it is a data collection / event collection environment with low overhead (less than 1% performance impact).

The JVM or OS can create data as events that are buffered and serialized in binary format, and then can be used for various purposes, such as troubleshooting.

JEP 329: ChaCha20 and Poly1305 Cryptographic Algorithms

ChaCha20 and Poly1305 algorithms are described in RFC 7539 and will be implemented in SunJCE provider.

JEP 330: Launch Single-File Source-Code Programs

It is now possible to run one Java file:

java MyHelloWorldApp.java

Additionally, you could define a file as executable, for that you have to add as the first line of that java file and change the file extension to other than .java:

#!/path/to/java -source 11

// your Java code here

JEP 331: Low-Overhead Heap Profiling

Using a new JVMTI SampledObjectAlloc callback, you can track particular or selected heaps and their location in a low overhead environment.

JEP 332: Transport Layer Security (TLS) 1.3

Provides support of TLS 1.3, but without 0-RTT data support, an authentication after communication and timestamps of the signed certificate (SCT) (RFC 6962).

JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)

Provided an experimental garbage collector with a low overhead, and named as Z Garbage Collector or ZGC.

Whose goal is a maximum pause time of 10 miles per second and an impact on performance of no more than 15% compared to the G1 garbage collector.

At that moment it will be supported for Linux/x64 platform only.

JEP 335: Deprecate the Nashorn JavaScript Engine

Nashorn was defined as deprecated with a future deletion.

JEP 336: Deprecate the Pack200 Tools and API

The compression tools Pack200, which are used for JAR files, are deprecated and will be deleted.

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022