Improved Emoji Support in Java 21 - Sip of Java
Billy Korando on November 20, 2023
Great news, Java developers, support for emojis has been improved in Java 21. Let’s take a look!
Emojitional Support
Before Java 21, there was no standard API for evaluating if a Character
was an emoji, even though this information was available in the CLDR data included in the JDK. As the usage of emojis has become increasingly common, JDK-8303018 was created to add API to java.lang.Character
to give Java developers the ability to check if emojis were being used.
Character API Updates
JDK-8303018 added six new methods to java.lang.Character
:
All six of the new methods are static
, take a character’s int codePoint
value, and return a boolean
value. At the base level, isEmoji(int)
can be used to check if a Character
is an emoji; the other methods can be used to provide information on how an emoji should be rendered.
Using the Emoji API
The new emoji API can be used directly, like in the example code below transforming a String
into an IntStream
and evaluating if it contains an emoji:
isEmojiModifierBase
and isEmojiModifier
can be used to see if an emoji can be modified and if it has been modified; in this example, the emoji “🙋🏻♂️” is a base emoji that has had a modification applied to it: ️
Emoji API Support in Regex
These new methods can also be accessed in regex through the property construct \p{IsXXXX}
like in this example:
Note the property construct uses a modified snake case to reference the methods, as seen in this example:
Additional Reading
JDK 21 JavaDoc for java.lang.Character
Unicode® Technical Standard #51
Happy coding!