Sitemap

Java 21 : New Features

3 min readJan 9, 2025

--

Hey, Java learners !! Java is the most trending language as a java programmer you most be agreed !!! So to make the Java programming more interesting, Java community keeps on enhancing it always.

JDK 21

Like we update our gadgets such as mobile phones from a old to new one for some interesting features, similarly we should adapt the latest tech too.

In this Article, we’ll discuss the new features and enhancements that had been added to Java 21. JDK 21 will receive updates under the NFTC, until September 2026, a year after the release of the next LTS.

New Features

1. Math.clamp() and StrictMath.clamp() Methods

it’s necessary to clamp a numerical value to a given
range. To achieve this earlier we can do like below :

int clampedValue = value > max ? max : value < min ? min : value;
or probably
int clampedValue = Math.max(min, Math.min(max, value)); But these “algorithms” are verbose and error-prone.

But in Java 21, methods Math.clamp() and StrictMath.clamp() are added to conveniently clamp the numeric value between the specified minimum and maximum values.Four overloads are provided in both Math and StrictMath classes for int, long, float, and double types. A clamp(long value, int min, int max) overload can also be used to safely narrow a long value to int.

2. New String indexOf(int,int,int) and indexOf(String,int,int) Methods to Support a Range of Indices

Two new methods indexOf(int ch, int beginIndex, int endIndex) and indexOf(String str, int beginIndex, int endIndex) are added to java.lang.String to support forward searches of character ch, and of String str, respectively, and limited to the specified range of indices.

Besides full control on the search range, they are safer to use than indexOf(int ch, int fromIndex) and indexOf(String str, int fromIndex), respectively, because they throw an exception on illegal search ranges.

3. New splitWithDelimiters() Methods Added to String and java.util.regex.Pattern

Get Debendra Dhinda’s stories in your inbox

Join Medium for free to get updates from this writer.

Unlike the split() methods, these new splitWithDelimiters() methods in java.lang.String and java.util.regex.Pattern return an alternation of strings and matching delimiters, rather than just the strings.

4. Sequenced Collections (JEP 431)

In the Java collection framework, no collection type represents a sequence of elements with a defined encountered order. For instance, List and Deque interfaces define an encounter order, but their common super type Collection doesn’t. In the same way, Set doesn’t define an encounter order, but subtypes such as LinkedHashSet or SortedSet do.

Java 21 introduced new interfaces to represent collections with a defined encounter order.Each such collection has a well-defined first element, second element, and so forth, up to the last element. It also provides uniform APIs for accessing its first and last elements, and for processing its elements in reverse order.

5. Pattern Matching for switch

This feature was originally proposed by JEP 406 (JDK 17) and subsequently refined by JEPs 420 (JDK 18), 427 (JDK 19), and 433 (JDK 20).

The main goal of this feature is to allow patterns in switch case labels and improve the expressiveness of switch statements and expressions. Besides, there is also an enhancement to handle NullPointerException by allowing a null case label.

Example :

static void testStringNew(String response) {
switch (response) {
case null -> { }
case String s
when s.equalsIgnoreCase("YES") -> {
System.out.println("You got it");
}
case String s
when s.equalsIgnoreCase("NO") -> {
System.out.println("Shame");
}
case String s -> {
System.out.println("Sorry?");
}
}
}

Conclusion

There are several other features introduced in java 21 also. Read more on https://www.oracle.com/java/technologies/javase/21-relnote-issues.html#NewFeature

Read More

--

--

Debendra Dhinda

Written by Debendra Dhinda

Technology Enthusiast —Java8, Spring Boot, Micro-services, DDD, Web, Mobile, and Cloud-Native. Passionate about designing and developing scalable software.