In Java, codePoints() returns an IntStream with one value per Unicode code point in the string. A character outside the Basic Multilingual Plane, such as the emoji U+1F600, is stored as two chars (a surrogate pair) but appears in this stream only once.
Use it when you need to count, inspect, or filter real characters: counting user-visible symbols, printing U+ codes, or testing letters that live outside the BMP. For plain ASCII text it gives the same values as chars().
codePoints() is an instance method of java.lang.String, usable since Java 8 (inherited from CharSequence; String overrides it since Java 9). Call it on a string, as in text.codePoints(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed. Requires Java 8 or newer.
Syntax
Call it with no arguments and chain stream operations; each value is an int code point.
java
long symbols = text.codePoints().count();
int[] cps = text.codePoints().toArray();
text.codePoints().filter(Character::isLetter).count();
Call
What it does
"ab".codePoints().count()
2.
"\uD83D\uDE00".codePoints().count()
1, although length() is 2.
"A".codePoints().findFirst().getAsInt()
65.
An unpaired surrogate char (broken text) is passed through as its own value. Combining marks, such as an accent typed as a separate code point, are still separate code points: one visible symbol can be more than one code point.
Library declarations
java
public IntStream codePoints();
Parameters
Parameter
Description
None
codePoints() takes no parameters.
Return Value
Return Value
Description
IntStream
The code points of the string, in order.
Java Examples
Example 1 — Count Characters That Include an Emoji
Compare length(), chars(), and codePoints() for a short message with a smiley.
java
public class CodePointsCount {
public static void main(String[] args) {
String s = "hi \uD83D\uDE00";
System.out.println("length(): " + s.length());
System.out.println("chars(): " + s.chars().count());
System.out.println("codePoints(): " + s.codePoints().count());
}
}
Output
length(): 5
chars(): 5
codePoints(): 4
How It Works
Escapes keep the source ASCII.\uD83D\uDE00 is the surrogate pair for the smiley U+1F600, written with Unicode escapes.
UTF-16 counts five.h, i, the space, and the two halves of the smiley give 5 for both length() and chars().count().
Code points count four.codePoints() merges the pair into one value, matching what a reader sees: h, i, space, smiley.
Example 2 — Print Each Code Point in U+ Notation
Show the code of every character and how many chars it occupies.
java
public class CodePointsHex {
public static void main(String[] args) {
String text = "A\u00E9\uD83D\uDE80";
text.codePoints().forEach(cp ->
System.out.println(String.format("U+%04X", cp) + " uses " + Character.charCount(cp) + " char(s)"));
}
}
Three symbols. The string holds A, an e with acute accent (U+00E9), and a rocket (U+1F680) stored as \uD83D\uDE80.
Hex formatting.%04X prints the code in uppercase hex padded to at least 4 digits; the rocket needs 5.
Storage size.Character.charCount returns 2 only for the rocket, the one code point above U+FFFF.
Example 3 — Count Letters Correctly with codePoints()
A mathematical letter outside the BMP is a letter as a code point, but its two surrogate halves are not.
java
public class CodePointsLetters {
public static void main(String[] args) {
String name = "Java\uD835\uDD38";
long byChars = name.chars().filter(Character::isLetter).count();
long byCodePoints = name.codePoints().filter(Character::isLetter).count();
System.out.println("letters via chars(): " + byChars);
System.out.println("letters via codePoints(): " + byCodePoints);
}
}
Output
letters via chars(): 4
letters via codePoints(): 5
How It Works
An extra capital letter.\uD835\uDD38 encodes U+1D538, a double-struck capital A that Unicode classifies as an uppercase letter.
Surrogates are not letters.chars() hands Character.isLetter the values 0xD835 and 0xDD38 separately; neither is a letter, so only J, a, v, a count.
Whole code points pass.codePoints() delivers 0x1D538 as one value, isLetter accepts it, and the count is 5.
Frequently Asked Questions
An IntStream of Unicode code points. Characters stored as surrogate pairs, such as most emoji, appear as a single value.
chars() returns UTF-16 units, so a surrogate pair gives two values. codePoints() returns one value per Unicode code point.
Use text.codePoints().count() or text.codePointCount(0, text.length()). length() counts UTF-16 chars instead.
Collect them into an int array and call new String(cps, 0, cps.length), or append each one with Character.toChars(cp).
Java 8, through the CharSequence default method. String overrides it since Java 9 with a faster version; your code does not change.