preface

On January 16, 2019, Java 16 was officially released. We are ready to download and use Java 16.

features

Vector API (incubation)

At run time, Vector denotes that Vector computations can be reliably compiled to support the best Vector hardware instructions on the CPU architecture, thus achieving better performance than equivalent scalar computations. Improved Java performance in CPU vector computation.

Enable c++ 14 language features

Allow the use of new C++ 14 features in C++ source code in the JDK, and give specific guidance on which features might be used in hot code.

Migrate from Mercurial to Git

Migrate the OpenJDK community source code base from Mercurial (Hg) to Git.

The migration to making

Host the Git repository for the OpenJDK community on GitHub. Along with JEP 357(moving from Mercurial to Git), this will move all repository OpenJDK projects to GitHub, including JDK feature versions and JDK updates from version 11 and later.

ZGC concurrent thread stack processing

Move ZGC thread stack processing from the safe point to the concurrent phase.

Unix-domain socket channel

Added UNIX-Domain (AF_UNIX) socket support for Socket channel and server-Socket Channel APIS in the Java.nio. Channels package.

Alpine Linux Port

On the X64 and AArch64 architectures, port the JDK to Alpine Linux and other Linux distributions that use MUSL as their primary C library.

Elastic Metaspace

The unused hot-class metadata (metaspace metspace) memory is returned to the operating system in a more timely manner, reducing the use of the metspace, and simplifying the metspace code to reduce maintenance costs.

Windows/AArch64 Port

Port the JDK to Windows/AArch64. With the release of new consumer and server grade AArch64 (ARM64) hardware, Windows/AArch64 has become an important platform for end user needs.

Foreign Linker API (Incubation)

Introduces an API that provides statically typed pure Java access to native code. This API, along with the external memory API (JEP 393), can greatly simplify the error-prone process of binding to a local library.

Warnings for value-based classes

Specify the original wrapper classes as value-based classes and deprecate their constructors for removal, with a new deprecation warning. Provides warnings about synchronization on any instance of a value-based class in the Java platform.

Packaging tools

Provides jPackage tools for packaging self-contained Java applications.

External Memory access API (third incubation)

Introduce an API that allows Java programs to access external memory outside of the Java heap safely and efficiently.

Pattern matching for Instanceof

Enhance the Java programming language with pattern matching of the Instanceof operator. Pattern matching allows common logic in a program, that is, conditional extraction of components from objects, to be expressed in a more concise and secure manner.

We used to use this, first determine the type, then cast.

if (obj instanceofString) { String s = (String) obj; . }Copy the code

In Java 16, you can use it this way, directly using the variable S.

if (obj instanceof String s) {
    // You can use s directly. }Copy the code

Also it can be used, but the premise is the first to judge, the second can be executed correctly, so you can use &&, cannot use | |.

if (obj instanceof String s && s.length() > 5) {
    flag = s.contains("jdk");
}
Copy the code

Records

The Java programming language is enhanced with the Records keyword, a class that acts as a transparent carrier of immutable data.

Previously we defined a class like this:

class Point {
    private final int x;
    private final int y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    int x(a) { return x; }
    int y(a) { return y; }

    public boolean equals(Object o) {
        if(! (oinstanceof Point)) return false;
        Point other = (Point) o;
        return other.x == x && other.y == y;
    }

    public int hashCode(a) {
        return Objects.hash(x, y);
    }

    public String toString(a) {
        return String.format("Point[x=%d, y=%d]", x, y); }}Copy the code

In Java 16, all you need is a succinct definition like this:

record Point(int x, int y) {}Copy the code

Strong encapsulation is implemented internally within the JDK by default

By default, all internal elements of the JDK are strongly encapsulated, except for key internal apis, such as Sun.misc.unsafe. Allow end users to opt for loose strong encapsulation, which has been the default since JDK 9. Improved JDK security and maintainability.

Sealed Classes

Enhance the Java programming language with sealed classes and interfaces. Sealed classes and interfaces limit how other classes or interfaces can extend or implement them.

public abstract sealed class SealedHuman permits Student.Teacher {
    protected final String name;
    public abstract void speak(a);
    public SealedHuman(String name) {
        this.name = name; }}public final class Student extends SealedHuman {
    public Student(String name) {
        super(name);
    }
    
    public void speak(a) {
        System.out.println("student name:" + name);
    }
    
    public void study(a) {
        System.out.println(name +" is studying"); }}public final class Teacher extends SealedHuman {
    public Teacher(String name) {
        super(name);
    }
    
    public void speak(a) {
        System.out.println("The Teacher name:" + name);
    }        

    public void teach(a) {
        System.out.println(name +"is teache"); }}Copy the code

To try out the new Java 16 features in advance, follow the following public account and add the author to obtain the Java 16 installation package and official guidance documentation.