features of java 21

The new features of Java 21 : Java Development Kit (JDK) 21

Java 21 is now available with millions of developers running over more than 60 billion Java Virtual Machines(JVM) worldwide with new features of java 21. Java continues to be the development platform of choice for developers and enterprises . This Java release improves the security, performance, and stability of Java application development. Here we are going to understand the comprehensive overview of the new features of java 21 introduced in Java Development Kit (JDK) 21, the newest long-term support (LTS) release of Oracle’s standard Java implementation as specified by JSR 396 in the Java Community Process. 

Features of Java 21

 

  1.  New splitWithDelimiters() Methods Added to String and java.util.regex.Pattern
  2.  The java.net.http.HttpClient Is Now AutoCloseable
  3.  Support for GB18030-2022
  4.  New StringBuilder and StringBuffer repeat Methods
  5.  Emoji Related Binary Properties in RegEx
  6.  Sequenced Collections
  7.  Warning Printed When an Agent Is Loaded into a Running VM
  8.  Support Searching for Section Headings in Generated Documentation
  9.  JDK Tool Access in JShell
  10.  -XshowSettings:locale Output Now Includes Tzdata Version
  11.  Changes to JAXP Configuration Files
  12.  New javac Warning When Calling Overridable Methods in Constructors
  13.  Generate “output file clash” Warning when an Output File is Overwritten During Compilation
  14.  Support Searching for Section Headings in Generated Documentation
  15.  Enhanced OCSP, Certificate, and CRL Fetch Timeouts
  16.  Support for HSS/LMS Signature Verification
  17.  SunJCE Provider Now Supports SHA-512/224 and SHA-512/256 As Digests for the PBES2 Algorithms
  18.  Support for Password-Based Cryptography in SunPKCS11
  19.  New System Property to Toggle XML Signature Secure Validation Mode
  20.  Update XML Security for Java to 3.0.2
  21.  Generational ZGC
  22.  Last Resort G1 Full GC Moves Humongous Objects
  23.  New JFR View Command 

Below are 30 new features of java 21 :

1. String Templates

One of the features of java 21 is String templates allow text and expressions to be composed without using the + operator. The result is going to be a string,  but it can also be an object of another type. Each string template has a template processor that validates the text and expressions before composing them, achieving greater safety than basic ‘string interpolation’ features in other languages.

The following example declares a template expression which uses the template processor STR and contains one embedded expression, name:
       String name = “TechShitanshu”;

       String info = STR.”My name is \{name}”;

        System.out.println(info);

    It prints the following:

        My name is TechShitanshu

2. Unnamed Classes and Instance Main Methods

This features of java 21 enable one to write streamlined declarations for single-class programs and then seamlessly expand their programs later to use more advanced features as their skills grow. Without the full ceremony of the class declaration, Unnamed classes allow the user to provide class content . The instance main method feature allows the user to drop the formality of public static void main(String[] args) and simply declare void main().

3. Runtime.exec and ProcessBuilder Logging of Command Arguments

This features of java 21 Processes started by Runtime.exec and ProcessBuilder can be enabled to log the command, arguments, directory, stack trace, and process id. The exposure of this information should be reviewed before implementation. Logging of the information is enabled when the logging level of the System#getLogger(String) named java.lang.ProcessBuilder is System.Logger.Level.DEBUG or Logger.Level.TRACE. When enabled for Level.DEBUG, only the process id, directory, command, and stack trace are logged. When enabled for Level.TRACE, the command arguments are included with the process id, directory, command, and stack trace.

4. System.exit() and Runtime.exit() Logging

Calls to java.lang.System.exit() and Runtime.exit() are logged to the logger named java.lang.Runtime with a logging level of System.Logger.DEBUG. When the configuration of the logger allows, the caller can be identified from the stack trace included in the log.

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

Both the methods Math.clamp() and StrictMath.clamp() are added to clamp conveniently the numeric value between the given 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.

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

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

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

7. Unicode Emoji Properties

Below six new methods are added to java.lang.Character for obtaining Emoji character properties, which are defined in the Unicode Emoji Technical Standard (UTS #51) :

  • isEmoji(int codePoint)
  • isEmojiPresentation(int codePoint)
  • isEmojiModifier(int codePoint)
  • isEmojiModifierBase(int codePoint)
  • isEmojiComponent(int codePoint)
  • isExtendedPictographic(int codePoint)

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

This 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.

9. The java.net.http.HttpClient Is Now AutoCloseable

The below methods have been added to the API:

  • void close():  It waits for submitted requests to complete and gently close the client.
  • void shutdown(): initiates a gentle shutdown, then returns immediately without waiting for the client to terminate.
  • void shutdownNow(): initiates an immediate shutdown, it will interrupt active operations, and returns immediately without waiting for the client to terminate.
  • boolean awaitTermination(Duration duration):  within the given duration waits for the client to terminate, returns true if the client is terminated, false otherwise.
  • boolean isTerminated(): If the client is terminated returns true .

The instances returned by HttpClient.newHttpClient(), and the instances built from HttpClient.newBuilder(), provide a best effort implementation for these methods. They also allow the reclamation of resources allocated by the HttpClient early, without waiting for its garbage collection.

Note that an HttpClient instance typically manages its own pools of connections, which it may then reuse when necessary. Connection pools are typically not shared between HttpClient instances. Creating a new client for each operation, though possible, will usually prevent reusing such connections.

1 thought on “The new features in Java 21 : Java Development Kit (JDK) 21”

  1. Pingback: The Major new functionality in Java 21 - TECH SHITANSHU

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top