Officially released Java 16


Preface

On March 16, 2021, Java 16 was officially released. We can download and use Java 16.

Insert picture description here

characteristic

Vector API (Incubating)

At runtime, Vector means that vector calculations can be reliably compiled to support the best vector hardware instructions on the CPU architecture, so as to achieve better performance than equivalent scalar calculations. Improved the performance of Java in CPU vector calculation.

Enable c++ 14 language features

Allows the use of new features of C++14 in the C++ source code in the JDK, and gives specific guidance on which features may be used in the hot code.

Migrating from Mercurial to Git

Migrate the source code repository of the OpenJDK community from Mercurial (hg) to Git.

Migrate to GitHub

Host the Git repository of the OpenJDK community on GitHub. Together with JEP 357 (Migrating from Mercurial to Git), this will migrate all OpenJDK projects in the repository to GitHub, including JDK feature versions and version 11 and later JDK updates.

ZGC concurrent thread stack processing

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

Unix-Domain socket channel

In the java.nio.channels package, add Unix-domain (AF_UNIX) socket support for socket channel and server-socket channel api.

Alpine Linux Port

On the x64 and AArch64 architectures, port the JDK to Alpine Linux and other Linux distributions that use musl as the main C library.

Elastic Metaspace

Return the unused hotspot metadata (i.e. metaspace metaspace) memory to the operating system in a timely manner to reduce the occupation of metaspace and simplify the metaspace code to reduce maintenance costs.

Windows/AArch64 Port

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

Foreign Linker API (Incubating)

Introduce an API that provides statically typed pure java access to native code. This API, together with the external memory API (JEP 393), can greatly simplify the error-prone process of binding to native libraries.

Warning for value-based classes

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

Packaging tool

Provide the jpackage tool for packaging self-contained Java applications.

External memory access API (third incubation)

Introduce an API to allow Java programs to safely and effectively access external memory outside the Java heap.

Pattern matching for instanceof

Use pattern matching on the instanceof operator to enhance the Java programming language. Pattern matching allows common logic in programs, that is, to conditionally extract components from objects, and express them in a more concise and safer way.

In the past, we used it like this, first determine the type, and then force conversion.

if (obj instanceof String) {
    
    
    String s = (String) obj; 
    ...
}

In Java 16, you can use the variable s directly.

if (obj instanceof String s) {
    
    
    // 可以直接使用s
    ...
}

It can also be used in this way, but the premise is that the first judgment is established and the second can be executed correctly, so you can use && instead of ||.

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

Records

Use the Records keyword to enhance the Java programming language, and Records as a class for the transparent carrier of immutable data.

Previously we defined a class like this, as follows:

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

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

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

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

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

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

In Java 16, only the following definitions are needed concisely:

record Point(int x, int y) {
    
     }

Strong encapsulation inside 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. End users are allowed to choose loose strong encapsulation, which is the default encapsulation since JDK 9. Improve the security and maintainability of the JDK.

Sealed Classes

Use sealed classes and interfaces to enhance the Java programming language. Sealed classes and interfaces limit other classes or interfaces to extend or implement them.

public abstract sealed class SealedHuman permits Student, Teacher {
    
    
    protected final String name;
    public abstract void speak();
    public SealedHuman(String name) {
    
    
        this.name = name;
    }
}

public final class Student extends SealedHuman {
    
    
    public Student(String name) {
    
    
        super(name);
    }
    
    public void speak() {
    
    
        System.out.println("student name:" + name);
    }
    
    public void study() {
    
    
        System.out.println(name +" is studying");
    }
}

public final class Teacher extends SealedHuman {
    
    
    public Teacher(String name) {
    
    
        super(name);
    }
    
    public void speak() {
    
    
        System.out.println("Teacher name:" + name);
    }        

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

jdk16 official explanation: https://www.oracle.com/news/announcement/oracle-announces-java-16-031621.html
official download address: https://www.oracle.com/java/technologies/javase-downloads .html

Guess you like

Origin blog.csdn.net/m0_50654102/article/details/115025675