DEV Community

Cover image for What are new Java 16 features?
saurabhshar
saurabhshar

Posted on • Originally published at campuscoding.com

What are new Java 16 features?

The latest version of java – open-source Java 16 (Java SE 16) and Java 16 Development Kit (JDK 16) was released recently. As this is the final release for Java 16; developers can download it and try the new version. In this article we’ll get to know about new Java 16 features introduced with this release.

Jdk 16 Download : https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

Jdk 16 release date : 16 March 2021

The main Java 16 features offered to users with the new release are 17 major enhancements (known as JEP – JDK Enhancement Proposals). This also includes 3 incubator modules and 1 preview feature, along with thousands of performance, stability and security changes.

Java 16 Features

  • JEP 338: Vector API (Incubator)
  • JEP 347: Enable C++ 14 Language Features
  • JEP 357: Migrate from Mercurial to Git
  • JEP 369: Migrate to GitHub
  • JEP 376: ZGC: Concurrent Thread-Stack Processing
  • JEP 380: Unix-Domain Socket Channels
  • JEP 386: Alpine Linux Port
  • JEP 387: Elastic Metaspace
  • JEP 388: Windows/AArch64 Port
  • JEP 389: Foreign Linker API
  • JEP 390: Warnings for Value-Based Classes
  • JEP 392: Packaging Tool
  • JEP 393: Foreign-Memory Access API
  • JEP 394: Pattern Matching for instanceof
  • JEP 395: Records
  • JEP 396: Encapsulate JDK Internals by Default
  • JEP 397: Sealed Classes

What are language enhancements appearing as new Java 16 features?

Records
Records were introduced as incubating feature in Java 14 & 15 as preview, and now finalized to be called a standard feature. Records are used to help with a compact syntax to declaring classes which are transparent holders for immutable data. Primarily, this helps in improving code readability and maintainability, it minimizes verbosity of these classes as well.

Pattern Matching for instanceof
This was also an incubating feature in previous releases and has become a standard Java 16 feature with new release. (In the past, this was also introduced as a preview feature in Java 14 and again in Java 15.)

Purpose – this feature allows you to express common logic in a program more securely i.e. now the conditional extraction of components from objects could be more concise and safe.

Java 16 features for JVM Improvements
Elastic Metaspace
This is expected to improve the usage of metaspace (memory space used by the class-metadata). It returns unused HotSpot VM metadata to the operating system immediately and so, less memory consumption and improved efficiency. This improves elasticity and improves the performance of the application and causes less memory utilization.

ZGC Concurrent Thread Processing
Move ZGC thread-stack processing from safe points to a concurrent phase, so that even for the large heaps all pauses and scalability issues are eliminated effectively This removes the only source of latency in the ZGC garbage collector and drastically improves performance and efficiency of applications in this and future releases.

New Tools and Libraries

Unix-Domain Socket Channels
Unix domain sockets are old features of Unix platforms(mostly!), These are now supported in Windows Server 2019 and in Windows 10 as well. The new Java 16 feature adds socket support in Unix domain (AF_UNIX) to the socket channel and server-socket channel APIs (part of java.nio.channels).

Packaging Tool
This assists in packaging self contained Java applications. It provides a way to generate platform native packages such as deb and rpm in Linux, pkg and dmg in macOS and msi and exe in Windows to give users a natural installation experience. These formats include pkg and dmg on macOS, exe and msi for Windows, and deb/rpm on Linux. This improves the end-user experience when installing apps and makes them easy to deploy.

You can now define launch-time parameters to at packaging time and this can be called directly either from the command line or by a program using the ToolProvider API. This feature initially came as an incubator feature in Java 14.

Note – Name of the jpackage module has changed from jdk.incubator.jpackage to jdk.jpackage

Futureproofing Your Work

Warning for Value-Based Classes
Primitive Wrapper class – java.lang.Double, java.lang.Integer, etc.

Value based Class – java.time.LocalDateTime, java.util.Optional etc.

This feature makes the primitive wrapper classes as value based and add forRemoval to constructors (deprecated since JDK 9). This prompts new warnings. If there is any incorrect attempt to synchronize on instances of value-based classes in the Java then there will be warnings provided for them.

Strongly Encapsulate JDK Internals by default
This feature, by default, enables strong encapsulation of all internal elements of the JDK (exception to this is critical internal APIs for ex. sun.misc.Unsafe). All the code that accesses internal JDK APIs and which previously compiled successfully with earlier releases, may not work by default anymore.

This feature is to encourage developers to start migrating from use of internal elements to standard java APIs. This was they can upgrade without issue to future Java releases.

Launcher option -–illegal-access directs strong encapsulation for JDK 9 and until JDK 15. From jdk 15 onwards this defaults to warning, and from JDK 16 it defaults to deny. For now, It’s still possible to relax encapsulation mildly for all packages with a single command-line option, however in future specific packages with –add-opens only will work.

Improving Productivity for OpenJDK Developers
These changes are primarily visible to developers working to develop OpenJDK and not to Java developers working to create applications.

*Move from Mercurial to Git
*Migrate to GitHub
OpenJDK Community’s source code repositories migrated from Mercurial to Git and host them on GitHub (applicable for JDK 11 and beyond).

New OpenJDK Git repositories – https://github.com/openjdk

Enable C++14 Language Features (in JDK source code)
This feature talks about the usage of C++14 language features in JDK C++ code.

Alpine Linux Port
Windows/AArch64 Port
The main idea behind these changes is not the porting effort but rather to integrate into main repository of java.

Tooling Support
Tooling support at present helps drive developer productivity. With Java 16, Oracle continues to welcome the work of prime IDE vendors who are offering offer developers support for current Java versions. Developers will continue to receive Java 16 support with these IDEs:

Eclipse Marketplace
JetBrains IDEA

Incubator and Preview Features

Foreign Linker API
Java has always supported native method calls via JNI (Java Native Interface) since long. JNI, however, is difficult and error prone. With this change JNI is replaced by a new API – a pure Java way to access native code. As of now this is an incubator feature. This API will make it much easier to bind and work with the native library.

Vector API
This is also an incubator API. It allows vector computations which can be compiled at runtime to efficient vector hardware instructions on supported CPU architectures. This way Vector API assists in getting better performance results as compared to the scalar computations.

Purpose – Using this API, Java developers will be able to easily write portable and high performance vector algorithms.

Sealed Classes (2nd Preview)
This is a preview feature. It restricts which other interfaces/classes may implement or extend them. Using this feature, the author of a class/ interface has a control over which code should implement it. It also provides a better way declaratively to restrict the use of a superclass than access modifiers.

Foreign Memory Access API (3rd Incubator)
This incubator API was first introduced in Java 14 and again in Java 15. This feature allows programs to efficiently operate on native, persistent, managed heap and other kid of kinds of foreign memory in a secured fashion. This also makes the basis for Foreign Linker API.

FAQ –

What is purpose of incubator modules?
Incubator modules are used to indicate developers about non-final APIs and non-final tools. This way users can offer feedback which can be taken again in the next version improving the quality of Java platform.

Preview features are fully specified and fully implemented Java features which are still impermanent. They are made available in JDK releases so that developers can have a sense of them and provide feedback based on their use cases. Based on this feedback loop, the feature could be made permanent in the future release as well. This way users are able to provide timely feedback and tool vendors(IDE for example) can have time to build support for such a feature even before number of Java developers start to use it in production.

Top comments (0)