What are the new features of Java-17?

On September 2021, Oracle released a new version of Java as Java-17. This is the LTS (long-term support) release. Following are the new features of java-17.

Restore Always-Strict Floating-Point Semantics:

  • We know the keyword ‘strictfp’ is used to ensure that we will get the same result for floating point value irrespective of platform. 
  • Previously we needed to use this keyword explicitly to call such behavior. 
  • From Java-17 onwards we don’t have to use this keyword

Enhanced Pseudo-Random Number Generators

  • Provide new interface types and implementations for pseudorandom number generators (PRNGs), including jumpable PRNGs and an additional class of splittable PRNG algorithms (LXM).
  • Following is an example of this new feature.
  • In the example, a stream of integer numbers will be returned based on the size provided. In the above example, we have provided a size of 3 hence 3 random values will be returned between the start value(inclusive) and the end value(exclusive).
  • We can add the algorithm based on our requirements.
  • Output for the above program is as follows

Deprecate the Applet API for Removal

  • Since many browsers have already removed their support for Java plugins and this API becomes irrelevant, java removed the API from java-17 onwards. 
  • This API was already deprecated in java-9.

Pattern Matching for the switch (Preview)

  • Java-17 added pattern matching in the switch expressions and statements. Let’s see the example for the same.

Output:


Sealed Classes

We have already seen the preview feature on sealed classes in java-15 and java-16. 

In Java-17 it is made available to the developer to use in production.

  • we can say that the class that cannot be inherited but can be instantiated is 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:


Foreign Function & Memory API (Incubator)

This will allow developers to access the code from outside the JVM and manage the memory out of the heap.

Using this feature we can now call the C library from Java code.


Vector API (Second Incubator)

  • This was introduced in java-16. This API is added to provide a means of vector computations that can perform more optimally than the traditional scalar method of computations.
  • Java -17 includes enhancements in response to feedback as well as performance improvements and other significant implementation enhancements.
  • Enhancements to the API to support operations on characters, such as for UTF-8 character decoding. Specifically, we add methods for copying characters between short vectors and char arrays, and new vector comparison operators for unsigned comparisons with integral vectors.
  • Enhancements to the API for translating byte vectors to and from boolean arrays.
  • Intrinsic support for transcendental and trigonometric lane-wise operations on x64 using Intel’s Short Vector Math Library (SVML).
  • General performance enhancements to the Intel x64 and ARM NEON implementations
  • 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. 

Context-Specific Deserialization Filters

  • Allow applications to configure context-specific and dynamically-selected deserialization filters via a JVM-wide filter factory that is invoked to select a filter for each deserialization operation.

Also, see:

-A blog by Shwetali Khambe

Related Posts