“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Introduction to the

On March 16, 2021, the JDK released a new version, JDK16, which is not the LTS version, but the precursor to the next LTS version, JDK17, with 17 major improvements, including new language features, new tools, improved memory management, and more.

So let’s take a look at what JDK16 has to offer.

New features for JDK16

In general, JDK16 has the following new features:

  • Some of the new features introduced in JDK14 were finally confirmed in JDK16.
  • Memory management improvements
  • New packaging tool
  • UNIX-Domain Socket channels
  • Warnings about value-based Classes
  • Encapsulating JDK Internals by default
  • C++ 14 language features are provided
  • Other preview versions of the new features

The following chart shows the number of new features in the JDK from 8 to 16:

As you can see, JDK8 and JDK9 are the most numerous, and there are basically few changes after that.

JDK8 introduces stream, lambda, generics, and a number of other very useful features. JDK9 introduces the new JPMS modular system, so there is a lot of change.

Relatively speaking, the changes after JDK10 are basically relatively small, and may also be related to the fixed 6 month release. After all, the time is relatively short, so the version changes are relatively small.

Note that JDK16 is not an LTS release, JDK17 released in September is! Stay tuned for my upcoming articles on new features in JDK17. As of now, LTS versions of JAVA are available in JDK8, JDK11, and JDK17. Which one are you using now?

Language improvement

JDK16 has two major language improvements: Pattern matching and records. Both of these new features were introduced as preview versions in JDK14 and eventually become final versions in JDK16.

If an object is an instanceof a class, you can use instanceof as an instance or subclass of that class. Returns true, false otherwise.

However, after judging, to use the corresponding object, it is necessary to perform type conversion as shown below:

If (site instanceof String){String stringSite = (String)site; System.out.println(stringSite.length()); }Copy the code

Pattern matching in JDK16

If (site instanceof String stringSite){system.out.println (stringsite.length ()); }Copy the code

Another final release is Records, introduced in JDK14 and 15. Records is a special Java class that represents structures for immutable objects.

Let’s look at a definition of Records:

public record Address(
        String addressName,
        String city
) {
}
Copy the code

AddressName = addressName; addressName = city; addressName = addressName; addressName = addressName; addressName = addressName; addressName = addressName; addressName = addressName; addressName = addressName; addressName = addressName; addressName = addressName

public record Address(String addressName, String city) { public Address(String addressName, String city) { this.addressName = addressName; this.city = city; } public String addressName() { return this.addressName; } public String city() { return this.city; }}Copy the code

It’s actually equivalent to the traditional:

public class AddressOld { private final String addressName; private final String city; public AddressOld(String addressName, String city) { this.addressName = addressName; this.city = city; } public String getAddressName() { return addressName; } public String getCity() { return city; }}Copy the code

But writing is much more convenient and simple.

Improvements in memory management

Looking at improvements in memory management, there are two main areas: Elastic Metaspace and ZGC’s concurrent thread stack handling.

The main function of Metaspace is to manage the memory for metadata of classes. Elastic Metaspace was introduced to improve the allocation and release of meta-space memory in the HotSpot JVM. Unwanted memory can be returned to the operating system more quickly, reducing overhead and memory fragmentation.

Elastic Metaspace allocates memory in smaller chunks and improves elasticity by returning unused Metaspace memory to the operating system. It improves performance and reduces maintenance costs.

So what is ZGC’s concurrent thread stack handling?

We know that ZGC is a low-latency garbage collection algorithm in HotSpot JVM. But there is always a constraint on stack processing in a thread called Safepoints. At SafePoints, Java threads pause execution, limiting the efficiency of GC.

ZGC’s concurrent thread stack processing ensures that Java threads can execute concurrently while GC Safepoints.

Unix-Domain Socket Channel

Socket communication is generally based on TCP/IP, but those familiar with Unix should know that in Unix everything exists as a file, even internal process communication.

Inter-process Communication (IPC) of Unix itself is the fastest and more secure way to communicate with processes on the same host.

Support for UNIX-Domain Socket channels has been added to JDK16.

Warning For Value-based Classes

What does this mean? We know that the primary type in Java has an Object type. For example, int corresponds to an Integer.

If the Integer constructor is used, we can construct it as follows:

 Integer integer= new Integer(100);
Copy the code

In JDK16, however, this constructor is deprecated:

    @Deprecated(since="9", forRemoval = true)
    public Integer(int value) {
        this.value = value;
    }
Copy the code

We can write it like this:

Integer integer2= 100;
Copy the code

Encapsulate the internal JDK package

In general, the packages we use are JDK public apis, but sometimes we use some classes that are not recommended for external use. JDK16 packages most of these classes, so you can use them directly in the standard JDK.

C++ 14 language features

This is the value of the underlying JDK C++ source code using C++ 14 language features, ordinary JDK users are not directly feel.

Preview new language features

Several preview language features have also been added to JDK16. Vector API and Sealed Classes

The idea of the Vector API is to provide a Vector computation method that ultimately performs better than traditional scalar computation methods on supported CPU architectures. What is vector computation? If you are familiar with PANDAS, you may know that it is very easy to compute a matrix in PANDAS, but it is very difficult to compute every element in pandas in Java, which is why the library is so popular.

Now JDK16 can also do this, let’s have a look.

Int [] x = {1, 2, 3, 4}; int[] y = {4, 3, 2, 1}; int[] c = new int[x.length]; for (int i = 0; i < x.length; i++) { c[i] =x[i] * y[i]; }Copy the code

If we want to multiply the numbers of two arrays, we can only iterate over each element. Now written:

        var vectorA = IntVector.fromArray(IntVector.SPECIES_128, x, 0);
        var vectorB = IntVector.fromArray(IntVector.SPECIES_128, y, 0);
        var vectorC = vectorA.mul(vectorB);
        vectorC.intoArray(c, 0);
Copy the code

We construct two Vector variables by calling mul on the Vector class.

FromArray takes three arguments, the first is the length of the vector, the second is the original array, and the third is the offset. Since an int has 4 bytes, we use SPECIES_128 here.

Sealed Classes is a concept introduced in JDK15 that indicates which Classes are allowed to inherit from a class:

public sealed class SealExample permits Seal1, Seal2{
}

public non-sealed class Seal1 extends SealExample {
}

public final class Seal2 extends SealExample {
}
Copy the code

Final means Seal2 can no longer be inherited. Non-sealed: allows inheritance of any class.

conclusion

These are the new features that JDK16 brings to us. Generally speaking, they are very useful. What do you think?

The article example is learn-Java-base-9-to-20

This article is available at www.flydean.com/26-jdk16-ne…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!