Introduction
On March 16, 2021, JDK ushered in a new version of JDK16. Although JDK16 is not an LTS version, as the next LTS version JDK17, JDK16 brings us improvements in 17 areas, including New language features, new tools, and memory management improvements have been introduced.
So let's take a look at what new features JDK16 provides for us.
New features of JDK16
In general, JDK16 has the following new features:
- Some new features introduced in JDK14 were finally confirmed in JDK16.
- Improved memory management
- New packaging tool
- UNIX-Domain Socket channels
- Warning of Value-based Classes
- Encapsulating JDK Internals by default
- Provides C++ 14 language features
- Some other new features in the preview version
The following figure shows the number of new features of JDK from 8 to 16:
It can be seen that JDK8 and JDK9 are the most, and there are basically fewer changes later.
JDK8 introduced a series of very useful features such as stream, lambda, and generics. And JDK9 introduced a new JPMS modular system, so there are many changes.
Relatively speaking, the changes after JDK10 are basically relatively small, and it may also be related to the fixed release of the version every 6 months. After all, the time is relatively short, so the version changes are relatively small.
Note that JDK16 is not an LTS version, it is the JDK17 released in September! , Everyone can pay attention to my follow-up article on the new features of JDK17. So far, the LTS version of JAVA has JDK8, JDK11 and JDK17. Which one are you using now?
Language improvement
There are two main language improvements in JDK16: pattern matching and records. Both of these new features were introduced as a preview version in JDK14, and finally became a final version in JDK16.
Let’s take a look at Pattern matching first. Pattern matching mainly refers to the instanceof keyword. We know that in JAVA to determine whether an object is an instance of a certain class, you can use instanceof. If it is an instance or subclass of the class, it returns true, otherwise it returns false.
But after the judgment, if you want to use the corresponding object, you also need to display the type conversion as shown below:
//传统写法
if(site instanceof String){
String stringSite = (String)site;
System.out.println(stringSite.length());
}
In Pattern matching in JDK16, you can write like this:
//JDK16写法
if(site instanceof String stringSite){
System.out.println(stringSite.length());
}
Another final version is Records introduced in JDK14 and 15. Records is a special java class mainly used to represent the structure of immutable objects.
Look at the definition of a Records:
public record Address(
String addressName,
String city
) {
}
Above we defined an Address object, which has two attributes, addressName and city. If you decompile the compilation result of the above code, you can get:
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;
}
}
In fact, it is equivalent to 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;
}
}
But it is much more convenient and simple to write.
Improvements in memory management
Looking at the improvement in memory management, there are two main aspects: Elastic Metaspace and ZGC's concurrent thread stack processing.
The main function of Metaspace is to manage the memory of class metadata. The introduction of Elastic Metaspace is to improve the allocation and release of metaspace memory in HotSpot JVM. Unneeded memory can be returned to the operating system faster, thereby 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 can improve performance and reduce maintenance costs.
So what is ZGC's concurrent thread stack processing?
We know that ZGC is a low-latency garbage collection algorithm in HotSpot JVM. But in the process of thread stack processing, there is always a restrictive factor is safepoints. At the point of safepoints, java threads are to be suspended, which limits the efficiency of GC.
The concurrent thread stack processing of ZGC can ensure that java threads can execute concurrently while GC safepoints.
Unix-Domain Socket Channel
Generally speaking, Socket communication is based on TCP/IP, but friends who are familiar with Unix should know that everything exists in the form of files in Unix, even in internal process communication.
If it is a process on the same host to communicate, using Unix's inter-process communication (IPC) is undoubtedly the fastest way and more secure.
Therefore, support for Unix-Domain Socket Channel was added in JDK16.
Warning For Value-based Classes
What does this mean? We know that the corresponding primary type in Java has an Object type, for example, int corresponds to Integer.
If we use Integer's constructor, we can construct it like this:
Integer integer= new Integer(100);
But in JDK16, this constructor has been deprecated:
@Deprecated(since="9", forRemoval = true)
public Integer(int value) {
this.value = value;
}
We can write directly like this:
Integer integer2= 100;
Encapsulate the internal JDK package
Generally speaking, the packages we use are all JDK public APIs, but sometimes some classes used in the JDK are still used. Such classes are not recommended to be used directly externally. JDK16 encapsulates most of these classes. , You can find and use it directly in the standard JDK.
C++ 14 language features
This is because the C++ source code at the bottom of the JDK uses the C++ 14 language feature, and the average JDK user cannot directly feel it.
Preview new language features
Several preview language new features have also been added to JDK16. Here we mainly talk about Vector API and Sealed Classes.
The idea of the Vector API is to provide a vector calculation method that can ultimately perform better than the traditional scalar calculation method (on the CPU architecture). What is vector calculation? Friends who are familiar with pandas may know that in pandas, matrices can be easily calculated. If implemented in java, each element in the matrix needs to be calculated, which is very troublesome. This is also the reason why the pandas library of python can be popular.
Now JDK16 can also do it, let's take a look together, first the traditional way of writing:
//传统写法
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];
}
If we want to multiply the numbers of two arrays, we can only traverse each element. Current writing:
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);
We construct two Vector variables and directly call the mul method of the Vector class.
There are three parameters in fromArray, the first is the length of the vector, the second is the original array, and the third is the offset. Because an int has 4 bytes, we use SPECIES_128 here.
Sealed Classes is a concept introduced in JDK15, which indicates which classes are allowed to inherit from a certain class:
public sealed class SealExample permits Seal1, Seal2{
}
public non-sealed class Seal1 extends SealExample {
}
public final class Seal2 extends SealExample {
}
final means that Seal2 can no longer be inherited. Non-sealed means that any class inheritance can be allowed.
Summarize
The above are the new features brought to us by JDK16. Generally speaking, they are very useful. What do you think?
This article example learn-java-base-9-to-20
This article has been included in http://www.flydean.com/26-jdk16-new-features/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。