Java is one of the most powerful programming languages in the software world. Recently, Java 16, the latest version of Java has been released on 16th March 2021.
Oracle announced the availability of Java 16 (JDK 16) with 17 new enhancements to the platform. This latest Java Development Kit release comes with language enhancements, new tools, and improved memory management to improve developer productivity and flexibility.
According to IDC’s latest report “Java Turns 25”, over nine million developers representing 69% of full-time developers worldwide use Java – this is more than any other language.
Java 16: What’s New?
JDK 16 offers performance, security, and stability updates along with 17 main changes that are known as JDK Enhancement Proposals – JEPs.
Key Highlights Of Java 16:
- Language enhancements that were introduced in JDK 14 and finalized in JDK 16
- Improved memory management
- New Packaging Tool
- UNIX-Domain Socket Channels
- Warning for Value-based Classes
- Encapsulating JDK Internals by default
- Enable C++ 14 Language Features
- Incubating and Preview Features
Language Enhancement:
Pattern matching and records were introduced in Java 14 as preview features and are now implemented in Java 16.
-
Pattern Matching For Instanceof:
The instanceof operator in Java is used to check object reference. It returns an either true or false values and checks whether an object belongs to a parent class, child class, or an interface. It is also used to check the type of object.
If this result returns true, the next step is to explicitly cast it to the specific type that is being compared. Pattern Matching for instance in Java helps remove redundant code of casting by introducing Pattern Matching variable.
With this JEP, the instanceof operator comes with a pattern variable. If the instanceof condition is true, the pattern variable binds to the variable being compared. This removes the need for explicit casting and allows conditional extraction of components from objects more safely and clearly.
-
Records:
Another feature that was introduced in Java 14 and Java 15 is Records. Record is a special Java class that offers a compact syntax for defining immutable data classes.
This will simplify passing immutable data between objects. Before this, it was a mundane task to pass immutable data as it required boilerplate methods and classes. Record simply designed to hold data in it.
Improved Memory Management:
These two new features are introduced in Java 16 – Elastic Metaspace and ZGC: Concurrent Thread-Stack Processing.
We will see these two features in detail.
-
Elastic Metaspace:
The primary function of Metaspace is to manage memory for class metadata. Elastic Metaspace is introduced to improve the allocation and deallocation of metaspace memory in the HotSpot JVM. This is used to return unneeded memory to the OS more quickly, thereby reducing overhead and memory fragmentation.
It allocates memory in smaller chunks and improves elasticity by returning unused metaspace memory to the OS. It leads to greater performance and reduced maintenance costs.
-
ZGC: Concurrent Thread-Stack Processing:
Oracle’s Z Garbage Collector is one of the low-latency collectors in the HotSpot JVM. This JEP moves ZGC thread-stack processing from safepoints to a concurrent phase.
New Packaging Tool:
This was also introduced in Java 14 as an incubator module. This new Packaging tool can be used for packaging self-contained Java applications. To give users a natural installation experience, it supports native packaging formats.
Unix-Domain Socket Channel:
Unix-Domain sockets are similar to TCP/IP sockets and are used for inter-process communication (IPC) on the same host OS. Unlike TCP/IP sockets, these sockets are addressed by file system pathnames rather than IP addresses and port numbers.
They are more secure and efficient than TCP/IP loopback connections. Java 16 adds support for all of the features of UNIX-domain sockets. Now, Java’s socket channel/server-socket channel API can use Unix domain sockets for faster and efficient IPC on the same host. Before Java 16, this was limited to TCP/IP sockets.
Warning For Value-based Classes:
This JEP designates the primitive wrapper classes as value-based and deprecates their constructors for removal, causing new deprecation warnings.
It provides warnings on improper attempts to synchronize on instances of any value-based classes.
Encapsulating JDK Internals By Default:
This is implemented with a goal to continuously improve the security and maintainability of the JDK. This JEP denotes the strong encapsulation of all internal elements of the JDK – except for some critical internal APIs.
This further encourages developers to migrate from using internal elements to using standard APIs, so developers can easily upgrade to future Java releases.
Enable C++ 14 Language Features:
This JEP enables the use of C++14 language features in JDK C++ source code. It also offers guidance on which of these features may be used in HotSpot code.
This JEP, along with JEP 357 and JEP 369 is focused to improve productivity for OpenJDK developers.
The abovementioned JEPs migrate the source code repositories of the OpenJDK community from Mercurial to Git and host OpenJDK’s Git repositories on GitHub.
Incubating And Preview Features:
-
Vector API:
This Incubator Vector API is an initial iteration of an incubator vector module. It expresses vector computations that compile at runtime to optimal vector hardware instructions. This vector API is based on the principle that Java developers should be able to use any native library that is required for a particular task.
-
Foreign Linker API:
This API offers statically typed, pure Java access to native code.
-
Foreign-Memory Access API:
It allows Java programs to safely and efficiently access foreign memory outside of the Java heap. It also provides the foundation for the Foreign Linker API.
-
Sealed Classes:
This second preview of sealed classes restricts which other classes or interfaces may extend or implement them. It allows the author to control which code is responsible for implementing it.