What’s new in Java 17?
We have decided to discuss Java 17 in this book as this is the next long-term support (LTS) version of Java, meaning that this version will be maintained for a more extended period. It was released on September 14, 2021, and includes several new security and development features.
Let’s look at some of the new features that have been included, as well as some modifications that have been applied to Java 17.
Sealed classes
Sealed classes were in the second preview stage in Java 16. Let’s say, for example, that we have a class that we have created in our code. We can assume that this class can be extended with any subclasses in our application. Thus, by making our classes sealed, we can ensure that our class can’t be extended by subclasses. If we want to allow some subclasses to extend our class, we can use the permits
keyword to identify the specific classes we want to grant permission to, as shown in the following example:
public sealed class Animal permits Cat, Dog, Horse
The foreign function and memory API
A new API was introduced for accessing and using code outside the Java runtime, which it did by applying foreign functions (code outside the JVM) and safely accessing foreign memory (memory not handled by the JVM). The API allows a Java application to call native libraries without the Java Native Interface (JNI).
The API aims to replace the JNI with a pure Java development model and better performance while accessing off-heap data and omitting insecure operations.
Foreign memory
One common problem in Java today is accessing off-heap data. Off-heap data is data that’s stored in memory outside the Java runtime. We can say that this is a third-party library. Accessing this data is very critical to performance in that Java garbage collectors work only on on-heap data, which lets them avoid the unpredictability of garbage collections. The following APIs are used to handle off-heap data:
- The ByteBuffer API: This API allows you to create direct ByteBuffers in off-heap data so that data can be managed outside the Java runtime. However, the major drawback of ByteBuffer is that its maximum size is 2 GB and that it is not deallocated promptly, causing the application’s runtime performance to slow down.
- The Sun.misc.Unsafe API: The Unsafe API exposes access operations that work on off-heap data. This API makes this process efficient since the Just-in-Time (JIT) compiler optimizes access operations. However, using the Unsafe API is discouraged as we are allowing access to any memory location.
- The Foreign Function and Memory API: This API solves the dilemma of accessing the memory’s location and sacrificing runtime performance as it provides classes and interfaces where applications can do the following:
- Allocate Foreign Memory
- Manipulate and access foreign memory
- Call Foreign Functions
Pattern matching with switch statements
Pattern matching is the idea of testing patterns and complicated expressions in switch statements. This new feature allows for more extensible and flexible usage of switch statements to accept complex expressions.
The Applet API
The Applet API is rarely used in Java as all browsers have removed support for Java browser plugins.
The experimental AOT and JIT compilers
The experimental Java-based ahead-of-time (AOT) and JIT compilers have been removed since their features have limited usage.
These are just some of the changes that have been applied to Java 17. Now, let’s learn about Angular, one of the top JavaScript frameworks today, and the advantages of using the Angular framework to develop your frontend.