Announcement Java 21 Released : JDK 21 Arrived- What's new features in Java 21
The latest version of Java 21 (JDK 21) has released on 19 September 2023. JDK 21 will be a long-term support (LTS) version, the most crucial Java release, probably ever, in that it opens up a whole new universe of possibilities for Java users. It provides a host of useful APIs and enhancements, the most notable addition, by far, is the additional support for the violent threads project Loom), Generational ZGC, Sequenced Collections, Record Patterns and Pattern Matching etc, Let's explore.
Java 21 release date :
19 September 2023Java 21 Download :
Java 21 JDK
Java 21 has below features coming out as part of the JDK 21:
-
JEP 430: String Templates (Preview)
-
JEP 431: Sequenced Collections
-
JEP 439: Generational ZGC
-
JEP 440: Record Patterns
-
JEP 441: Pattern Matching for switch
-
JEP 442: Foreign Function & Memory API (Third Preview)
-
JEP 443: Unnamed Patterns and Variables (Preview)
-
JEP 444: Virtual Threads
-
JEP 445: Unnamed Classes and Instance Main Methods (Preview)
-
JEP 446: Scoped Values (Preview)
-
JEP 448: Vector API (Sixth Incubator)
-
JEP 449: Deprecate the Windows 32-bit x86 Port for Removal
-
JEP 451: Prepare to Disallow the Dynamic Loading of Agents
-
JEP 452: Key Encapsulation Mechanism API
-
JEP 453: Structured Concurrency (Preview)
JEP 433: Pattern Matching for switch (Fourth Preview)
With the help of a new feature in JDK 21, developers may now more easily write code that checks
an object's type: pattern matching for instanceof
. If a developer wished to carry
out an
operation based on the type of an object in a previous version of Java, they had to use an
if-else
statement or switch statement. This procedure is streamlined, for instance,
using
pattern matching. Here's an example:
public static void main(String[] args) {
Object obj = "Hello, Java!";
if (obj instanceof String s && s.length() > 0) {
System.out.println("Non empty string!");
}
}
The if
statement in the example above determines whether the object is a String
instance and
whether the String's length is higher than 0. The statement "Non empty string!"
is
printed if both conditions are met.
JEP 434: Foreign Function & Memory API (Second Preview)
Java programmers are faced challenges with a choices when it comes to accessing off-heap data: Should they go the safe but ineffective way (ByteBuffer) or should they forgo safety in favour of performance (Unsafe)?
Java 19 has introduced an API that allows Java programmes to interact with code and data that is not part of the Java runtime. The API enables Java programmes to call native libraries and handle native data without the brittleness and danger of JNI by effectively executing foreign functions (i.e., code outside the JVM) and safely accessing foreign memory (i.e., memory not maintained by the JVM).
-
Ease of use - Use a superior, pure-Java programming approach instead of the Java Native Interface (JNI).
-
Performance - Offer performance that is on par with, if not superior to, that of currently available APIs like JNI and sun.misc.Unsafe.
-
Generality - Provide methods for operating on various types of foreign memory (e.g., native memory, persistent memory, and managed heap memory) as well as, in time, other platforms (e.g., 32-bit x86) and foreign functions written in languages other than C (e.g., C++, Fortran).
-
Safety - Permit programmes to execute risky operations on external memory, but by default, alert users about these activities.
JEP 432: Record Patterns (Second Preview)
Records, a new feature in JDK 21, offer a shortened method of declaring classes that are primarily used for data storage. Classes and records are similar, however records are shorter and more focused on data representation. Here's an example:
public record Employee(String name, int Id) {
public Employee {
if (Id < 0) {
throw new IllegalArgumentException("Id can't have a negative.");
}
}
}
In the example above, a record with the name "Employee"
and the fields "name"
and "Id"
was
generated. A check to make sure the Id
is not negative is included in the constructor for the
record. The record can be created in the following ways:
Employee emp = new Employee("Robin", 25);