DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Trending

  • Security by Design: Building Full-Stack Applications With DevSecOps
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  1. DZone
  2. Coding
  3. Java
  4. Exploring Java 21's Intriguing Additions (Part 1)

Exploring Java 21's Intriguing Additions (Part 1)

Learn about a few notable features that stand out in the upcoming Java 21, in an overview of their functionalities and potential use cases.

By 
A N M Bazlur Rahman user avatar
A N M Bazlur Rahman
DZone Core CORE ·
Jun. 30, 23 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
9.2K Views

Join the DZone community and get the full member experience.

Join For Free

Java 21 is going to introduce several intriguing additions that enhance the language's capabilities. This article aims to discuss a few notable features that stand out, providing an overview of their functionalities and potential use cases.

Character Enhancements

  1. Character.isEmoji(int codePoint): This method determines whether a character is considered an emoji based on its Unicode properties defined in Unicode Emoji (Unicode Emoji Technical Standard #51). For example, executing Character.isEmoji(9203) would return true for the character ⏳, which has the code point 9203.
  2. Character.isEmojiPresentation(int codePoint): Similar to isEmoji(), this method checks if a character has the Emoji Presentation property. It helps identify characters that display as emojis when rendered.

Example:

boolean isEmoji = Character.isEmoji(9203);
System.out.println(isEmoji);


The output will be true because the character with code point 9203 (⏳) is considered an emoji.

You can find more details about these methods in the Java documentation.

StringBuffer and StringBuilder Improvements

Java 21 enhances the StringBuffer and StringBuilder classes with the repeat() method, which allows repetitive concatenation of characters or character sequences. The two overloaded versions of repeat() are as follows:

  1. StringBuffer.repeat(int codePoint, int count): Repeats count copies of the string representation of the specified codePoint argument in the StringBuffer sequence; for instance, executing buffer.repeat(9203, 5) would append ⏳ five times to the StringBuffer, resulting in "⏳⏳⏳⏳⏳"
  2. StringBuffer.repeat(CharSequence cs, int count): Appends count copies of the specified CharSequence cs to the StringBuffer; Similarly, these methods are also available in the StringBuilder class.

Example:

var buffer = new StringBuffer();
buffer.repeat(9203, 5);
System.out.println(buffer);


The output will be ⏳⏳⏳⏳⏳, as the code point 9203 (⏳) is repeated five times using the repeat() method.

String Enhancements

Java 21 introduces two new methods in the String class, namely indexOf(String str, int beginIndex, int endIndex) and indexOf(int ch, int beginIndex, int endIndex), which expand the searching capabilities within a specified range of a string.

  1. String.indexOf(String str, int beginIndex, int endIndex): Returns the index of the first occurrence of the specified substring within the given index range of the string; This method provides the same result as invoking s.substring(beginIndex, endIndex).indexOf(str) + beginIndex. If the indexOf(String) method returns a non-negative index, it is returned; otherwise, -1 is returned.
  2. String.indexOf(int ch, int beginIndex, int endIndex): This returns the index of the first occurrence of the specified character within the given range of the string. The search starts at beginIndex and stops before endIndex. This method supports characters in the range from 0 to 0xFFFF, as well as other Unicode code points.

Java 21 also enhances the String class with a splitWithDelimiters(String regex, int limit) method. This method splits a string based on a given regular expression and returns both the strings and the matching delimiters. For example:

var booAndFoo = "boo:::and::foo";
String[] splits = booAndFoo.splitWithDelimiters(":+", 3);
System.out.println("splits = " + Arrays.toString(splits));


The output will be: splits = [boo, :::, and, ::, foo], where the string is split around the :+ delimiter, limiting the result to a maximum of three splits.

Collections Framework Enhancements

Java 21 introduces several new methods in the Collections class that enhance the Collections Framework's functionality.

  1. Collections.newSequencedSetFromMap(SequencedMap<E, Boolean> map): This method returns a SequencedSet backed by the specified SequencedMap. The resulting set maintains the ordering, concurrency, and performance characteristics of the backing map. This enables developers to obtain a SequencedSet implementation corresponding to any SequencedMap implementation.
  2. Collections.shuffle(List<?> list, RandomGenerator rnd): This method randomly permutes the elements of the specified list using the provided source of randomness. The permutations occur with equal likelihood, assuming a fair source of randomness.
  3. Collections.unmodifiableSequencedCollection(SequencedCollection<? extends T> c): This returns an unmodifiable view of the specified SequencedCollection. It allows read-only access to the underlying collection, throwing an UnsupportedOperationException if modification is attempted.
  4. Collections.unmodifiableSequencedSet(SequencedSet<? extends T> s): Returns an unmodifiable view of the specified SequencedSet, providing read-only access to the set while disallowing modifications
  5. Collections.unmodifiableSequencedMap(SequencedMap<? extends K, ? extends V> m): Similar to the previous methods, this returns an unmodifiable view of the specified SequencedMap, allowing read-only operations.

SequencedCollection and SequencedMap are new interfaces introduced in Java 21 that preserve the order of elements and entries, respectively.

To know more about Sequence Collection, read Collections Framework Makeover.

HashMap and HashSet Improvements

Java 21 introduces convenient factory methods for creating HashMap and HashSet instances.

  1. Collections.newHashMap(int numMappings): This creates a new, empty HashMap suitable for the expected number of mappings. The map uses a default load factor of 0.75, and its initial capacity is typically large enough to accommodate the expected number of mappings without requiring resizing.
  2. Collections.newHashSet(int numElements): This creates a new, empty HashSet suitable for the expected number of elements. The set also uses a default load factor of 0.75, with an initial capacity that can accommodate the expected number of elements efficiently.

Conclusion

If you want to experiment with these new features can download the OpenJDK from the OpenJDK JDK 21 Early-Access Builds.

Another alternative is to use SDKMan, a software development kit manager, to download and manage different versions of Java. SDKMan can be used via the command line, which can make the process easier for developers who prefer this method.

However, these are early-access builds, so they may not be as stable as the final release, scheduled for September 2023, and are intended for testing and feedback purposes.

Nonetheless, Java 21 introduces various exciting language and collections framework additions. The new methods related to character properties, string manipulation, and collections provide developers with enhanced functionality and flexibility in their code. These additions aim to improve the productivity and performance of Java developers.

Note: To explore the complete set of new functionalities introduced in Java 21, stay tuned for future articles discussing additional features.

Don't forget to share this post!

Code point Java Development Kit Java (programming language) Strings Data Types

Published at DZone with permission of A N M Bazlur Rahman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!