What are the new features of java-16?

On March 2021, Oracle released a new version of Java as Java-16. This is a short-term release for the JDK platform. Following are the new features of java-16.

Vector API (Incubator)

  • This API is added to provide a means of vector computations that can perform more optimally than the traditional scalar method of computations.
  • Let’s compare the old method and the new method to multiply two arrays.
  • Traditional method:
  • Using vector API:

Output:

  • In the above example, we are creating two IntVectors. 
  • The first parameter is the size of the vector(since the int value requires 4 bytes, 4 int values will require 16 bytes that is 128 bits).
  • The second parameter is an array and the third one is an offset. 

Enable C++14 Language Features

  • Allow the use of C++14 language features in JDK C++ source code, and give specific guidance about which of those features may be used in HotSpot code.
  • To take advantage of C++14 language features some build-time changes are required, with specifics depending on the platform compiler. 
  • Minimum acceptable versions of the various platform compilers also need to be specified.

ZGC: Concurrent Thread-Stack Processing

  • This is to move ZGC thread-stack processing from safepoints to a concurrent phase.

Foreign Linker API (Incubator)

  • This is to provide a flexible way to access native code on the host machine. 
  • Initially, for C language interoperability, in the future, it may be adaptable to other languages such as C++ or Fortran.

Warnings for Value-Based Classes

  • Designate the primitive wrapper classes as value-based and deprecate their constructors for removal, prompting new deprecation warnings.
  • Provide warnings about improper attempts to synchronize on instances of any value-based classes in the Java Platform.

Packaging Tool

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

Foreign-Memory Access API (Third Incubator):

  • Introduce an API to allow Java programs to safely and efficiently access foreign memory outside of the Java heap.
    • A new VarHandle API, to customize memory access var handles
    • Support for parallel processing of a memory segment using the Spliterator interface
    • Enhanced support for mapped memory segments
    • Ability to manipulate and dereference addresses coming from things like native calls

Pattern Matching for instanceof

  • This preview feature is introduced in java-14 and the same is continued in java-15 with no new enhancements.
  • In java-16 this feature is available for developer use.
  • This feature is to remove a lot of boilerplate code that typically comes with the instanceof operator:
  • The pattern-matching feature simplifies this by introducing a new binding variable:
  • Old code:
  • New code after pattern-matching is introduced.

Records

  • This feature was originally introduced by java-14 as a preview feature. Java-16 brings some incremental changes.
  • This is added to enhance the Java programming language with records, which are classes that act as transparent carriers for immutable data. Records can be thought of as nominal tuples. 
  • Let’s see examples with and without Records.
  • Without the record, we would create an immutable data transfer object (DTO) as shown in the below example.

Output: 

  • With Records

Output:

  • As we can see from the above 2 examples, without the record feature we need to write too much code for making a immutable class, and to print the class object we need to override the toString method explicitly.
  • As we can see from the second example the class definition has a new syntax specific for records. In the header, we provide the details about the fields inside the record.
  • Using this header, the compiler can infer the internal fields. Hence we don’t have to define specific member variables and accessors, as they’re provided by default. We also don’t have to provide a constructor.
  • Also, the compiler provides sensible implementations for the toString, equals, and hashCode methods.

Strongly Encapsulate JDK Internals by Default

  • Strongly encapsulate all internal elements of the JDK by default, except for critical internal APIs such as sun.misc.Unsafe.
  • Allow end users to choose the relaxed strong encapsulation that has been the default since JDK 9.

Sealed Classes (Second Preview)

  • Java-15 introduced the concept of sealed classes. Java-16 also continued it as a preview feature. It is a preview feature. Java-sealed classes and interfaces restrict which classes and interfaces may extend or implement them.
  • we can say that the class that cannot be inherited but can be instantiated is known as the sealed class.
  • It allows classes and interfaces to have more control over their permitted subtypes.
  • Note that the concept of sealed classes is a preview feature, not a permanent feature.

Example:


Also, see:

-A blog by Shwetali Khambe

Related Posts