Java grapheme
last modified November 18, 2026 Java 25
In this article we work with graphemes in Java. We show the difference between
UTF-16 chars, code points, bytes, and graphemes, and how to split, count, and
reverse text by graphemes with BreakIterator and the
\X regular expression construct.
Grapheme
A grapheme is the smallest unit of a writing system of any given language. It may consist of a single letter or of several combined elements, such as a base letter with accents or diacritics, that form one visually distinct symbol. A grapheme does not have to carry meaning on its own.
Historically, the term character referred to a single symbol of the ASCII table, which supports only 128 characters. As digital text evolved, the Unicode standard was introduced to cover the diversity of written languages, including complex scripts and emojis. In Unicode, what a user perceives as a single character may be made of several code points.
Unicode
Unicode is a standard for the consistent encoding, representation, and handling of text across different writing systems. It unifies the representation of letters, digits, punctuation, mathematical symbols, and emojis of practically all languages.
In Java, a String is a sequence of UTF-16 code units, each stored
in a 16-bit char. The length method returns the
number of these code units, not the number of characters a user sees. (Since
Java 9, compact strings store Latin-1 only text with one byte per character
internally, but the API still behaves as UTF-16.)
A code point is the number assigned to a Unicode character. For
example, the Latin letter Q has the U+0051 code point and the
Cyrillic small letter ж has U+0436.
A grapheme cluster consists of one or more code points that are
displayed as a single unit. For example, the Hindi syllable ते is
composed of two code points: U+0924 for त and
U+0947 for the vowel sign े. The rules for finding
grapheme cluster boundaries are defined in Unicode Standard Annex #29 as
extended grapheme clusters.
The stored representation of a string consists of bytes. Depending on the encoding, such as UTF-8, UTF-16, or UTF-32, a code point takes a different number of bytes. In UTF-8, a code point takes one to four bytes.
Unicode table
Below is a small portion of the Unicode table, showing code points, characters, and their names:
| Code point | Character | Name |
|---|---|---|
| U+0041 | A | LATIN CAPITAL LETTER A |
| U+0061 | a | LATIN SMALL LETTER A |
| U+03B1 | α | GREEK SMALL LETTER ALPHA |
| U+0416 | Ж | CYRILLIC CAPITAL LETTER ZHE |
| U+05D0 | א | HEBREW LETTER ALEF |
| U+0924 | त | DEVANAGARI LETTER TA |
| U+1F600 | 😀 | GRINNING FACE |
Surrogate pairs
UTF-16 uses 16-bit code units. A single code unit can represent every character
of the Basic Multilingual Plane (BMP), ranging from
U+0000 to U+FFFF. The BMP includes widely used
scripts like Latin, Greek, Cyrillic, Arabic, Hebrew, Devanagari, Chinese,
Japanese, and Korean, as well as common symbols and punctuation.
Characters beyond the BMP, from U+10000 to U+10FFFF,
are represented with surrogate pairs. A surrogate
pair is a pair of 16-bit code units that together encode one code point.
The first unit is the high surrogate (D800–DBFF),
the second one is the low surrogate (DC00–DFFF).
Most emojis, historic scripts, and many rare CJK ideographs lie outside the BMP.
| Code point | Character | High surrogate | Low surrogate | Name |
|---|---|---|---|---|
| U+1F600 | 😀 | D83D | DE00 | GRINNING FACE |
| U+1F680 | 🚀 | D83D | DE80 | ROCKET |
| U+1F4A9 | 💩 | D83D | DCA9 | PILE OF POO |
| U+1F60D | 😍 | D83D | DE0D | SMILING FACE WITH HEART-EYES |
| U+1F47B | 👻 | D83D | DC7B | GHOST |
Each of these emojis is a single code point, but it occupies two
char values in a Java string. A surrogate pair is not the same as
a grapheme: a grapheme may consist of several code points, some of which are
surrogate pairs. For example, a thumbs up with a skin tone modifier
👍🏽 is two code points (four chars) but one grapheme.
Java grapheme example
In the first example, we look at a Hindi word from several angles: its code
points, its UTF-8 bytes, and its graphemes. The examples use compact source
files and instance main methods, which are final in Java 25; classes such as
BreakIterator and StandardCharsets are imported
automatically.
void main() {
String word = "नमस्ते";
IO.println(word);
IO.println();
IO.println("Code points:");
word.codePoints().forEach(cp ->
System.out.printf("U+%04X %s%n", cp, Character.toString(cp)));
IO.println();
IO.println("UTF-8 bytes:");
byte[] bytes = word.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
System.out.printf("%02X ", b);
}
IO.println();
IO.println();
IO.println("Graphemes:");
BreakIterator it = BreakIterator.getCharacterInstance();
it.setText(word);
int count = 0;
int start = it.first();
for (int end = it.next(); end != BreakIterator.DONE; end = it.next()) {
IO.println(word.substring(start, end));
start = end;
count++;
}
System.out.printf("The word has %d graphemes%n", count);
}
The program prints the Hindi word namaste, its code points, its UTF-8 bytes, and its graphemes, which it also counts.
IO.println("Code points:");
word.codePoints().forEach(cp ->
System.out.printf("U+%04X %s%n", cp, Character.toString(cp)));
The codePoints method returns an IntStream of the
code points of the string. Unlike iterating over char values, it
correctly joins surrogate pairs into a single code point. The
Character.toString(int) method converts a code point back to a
string.
IO.println("UTF-8 bytes:");
byte[] bytes = word.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
System.out.printf("%02X ", b);
}
The getBytes method encodes the string into a sequence of bytes
using the given charset. This is how the word looks when written to a UTF-8
file or sent over the network. We print the bytes in hexadecimal notation;
each Devanagari code point takes three bytes in UTF-8.
BreakIterator it = BreakIterator.getCharacterInstance();
it.setText(word);
int count = 0;
int start = it.first();
for (int end = it.next(); end != BreakIterator.DONE; end = it.next()) {
IO.println(word.substring(start, end));
start = end;
count++;
}
The BreakIterator.getCharacterInstance method returns an iterator
over character boundaries, that is, grapheme cluster boundaries. The
first method returns the first boundary (index 0), and each call
to next returns the following boundary, or
BreakIterator.DONE at the end of the text. The
substring method extracts the grapheme between two consecutive
boundaries.
The word has six code points but only three graphemes: न, म, and स्ते. The last
one is a conjunct: the virama ् (U+094D) joins the consonants स and त into a
single cluster, which is followed by the vowel sign े. Since Java 20,
BreakIterator follows the extended grapheme cluster rules, and
since Java 22, which supports Unicode 15.1, it also keeps Indic conjuncts
together (rule GB9c). Older Java versions split the word differently.
$ java Main.java नमस्ते Code points: U+0928 न U+092E म U+0938 स U+094D ् U+0924 त U+0947 े UTF-8 bytes: E0 A4 A8 E0 A4 AE E0 A4 B8 E0 A5 8D E0 A4 A4 E0 A5 87 Graphemes: न म स्ते The word has 3 graphemes
Chars, code points, and graphemes
The three ways of measuring a string often give different results. The next example compares them for several words.
List<String> graphemes(String text) {
var it = BreakIterator.getCharacterInstance();
it.setText(text);
var result = new ArrayList<String>();
int start = it.first();
for (int end = it.next(); end != BreakIterator.DONE; end = it.next()) {
result.add(text.substring(start, end));
start = end;
}
return result;
}
void main() {
String[] words = {
"falcon", "cafe\u0301", "😀", "👍🏽", "🇸🇰", "👨👩👧", "नमस्ते"
};
for (String word : words) {
System.out.printf("%s: %d chars, %d code points, %d graphemes%n",
word, word.length(), word.codePointCount(0, word.length()),
graphemes(word).size());
}
}
The graphemes helper method splits a string into a list of
graphemes. The length method returns the number of UTF-16 chars,
and codePointCount returns the number of code points in the given
range.
"cafe\u0301"
The word café is written in decomposed form: the letter e followed by the
combining acute accent U+0301. We use the \u0301 escape so that the
source file editor does not silently normalize it. It looks the same as the precomposed é
(U+00E9), but it has one more code point. The accent belongs to the same
grapheme as the e.
"👍🏽", "🇸🇰", "👨👩👧"
The thumbs up emoji is followed by a skin tone modifier. The Slovak flag is made of two regional indicator symbols. The family emoji consists of three person emojis joined with two zero width joiners (U+200D). In all three cases, each code point outside the BMP takes two chars, yet the whole sequence is a single grapheme.
$ java Main.java falcon: 6 chars, 6 code points, 6 graphemes café: 5 chars, 5 code points, 4 graphemes 😀: 2 chars, 1 code points, 1 graphemes 👍🏽: 4 chars, 2 code points, 1 graphemes 🇸🇰: 4 chars, 2 code points, 1 graphemes 👨👩👧: 8 chars, 5 code points, 1 graphemes नमस्ते: 6 chars, 6 code points, 3 graphemes
Graphemes with regular expressions
Java regular expressions support the \X construct, which matches
one extended grapheme cluster. It is a concise alternative to
BreakIterator.
void main() {
String text = "Hi 👋🏻 from 🇸🇰!";
Pattern pattern = Pattern.compile("\\X");
Matcher matcher = pattern.matcher(text);
List<String> graphemes = new ArrayList<>();
while (matcher.find()) {
graphemes.add(matcher.group());
}
IO.println(graphemes);
IO.println("Number of graphemes: " + graphemes.size());
IO.println("Number of chars: " + text.length());
}
Each call to find matches the next grapheme of the text, and
group returns it. The waving hand with a skin tone modifier and
the flag are each matched as a single grapheme, even though together they take
eight chars. Java also supports the \b{g} boundary matcher, which
matches a grapheme cluster boundary.
$ java Main.java [H, i, , 👋🏻, , f, r, o, m, , 🇸🇰, !] Number of graphemes: 12 Number of chars: 18
Reversing a string by graphemes
Operations such as reversing or truncating a string should work on graphemes; otherwise, combining marks and emoji modifiers get separated from their base characters.
String reverseGraphemes(String text) {
var it = BreakIterator.getCharacterInstance();
it.setText(text);
var sb = new StringBuilder();
int end = it.last();
for (int start = it.previous(); start != BreakIterator.DONE; start = it.previous()) {
sb.append(text, start, end);
end = start;
}
return sb.toString();
}
void main() {
String text = "noe\u0308l 👍🏽";
IO.println(text);
IO.println(new StringBuilder(text).reverse());
IO.println(reverseGraphemes(text));
}
The text contains the letter e followed by the combining diaeresis U+0308 and
a thumbs up emoji with a skin tone modifier. The reverseGraphemes
method walks the boundaries backwards with last and
previous and appends each grapheme to a
StringBuilder.
The StringBuilder.reverse method keeps surrogate pairs intact, so
no invalid chars are produced. However, it reverses code points, not
graphemes: the diaeresis moves onto the l and the skin tone modifier is
separated from the thumb. The grapheme-based reversal gives the expected
result.
$ java Main.java noël 👍🏽 🏽👍 l̈eon 👍🏽 lëon
Source
Java BreakIterator, Java Pattern, Unicode Text Segmentation (UAX #29)
In this article we have worked with graphemes, code points, bytes, and
surrogate pairs of Unicode strings in Java. We have split, counted, and
reversed text by graphemes using BreakIterator and the
\X regex construct. See also the
Java String,
Java Character,
Java StringBuilder, and
Java regular expressions articles.
Author
List all Java tutorials.