STRING INSTANCE METHODS
Java String codePoints() method
codePoints() returns IntStream stream containing the string's Unicode code points. This is convenient when a simple char may be too limited, for example with emoji.
A stream of code points
String tekstas = "Java";
tekstas.codePoints()
.forEach(System.out::println);74
97
118
97
- The variable
tekstasstores the value Java. codePoints()creates anIntStreamstream.- Each stream element is one Unicode code point.
forEach(System.out::println)prints each code point on a separate line.
Why is codePoints() an instance method?
codePoints() is called on a specific string because the stream is created from its contents:
IntStream srautas = tekstas.codePoints();
We therefore do not write String.codePoints(). The method needs a specific String value from which to read Unicode code points.
Method syntax
public IntStream codePoints()
| Part | Meaning |
|---|---|
| Calling object | tekstas in the line tekstas.codePoints() |
| Parameters | No parameters |
| Return type | IntStream |
| Stream values | int Unicode code points |
| Surrogate pairs | Correctly combined into one code point |
| Empty string | Returns an empty stream |
The difference from length()
String tekstas = "A😀B";
System.out.println(tekstas.length());
System.out.println(tekstas.codePoints().count());4
3
length() counts UTF-16 char units. codePoints().count() counts Unicode code points. The text A😀B has 4 UTF-16 units but 3 code points: A, 😀 and B.
Code points in an array
import java.util.Arrays;
String tekstas = "Java";
int[] kodai = tekstas.codePoints().toArray();
System.out.println(Arrays.toString(kodai));[74, 97, 118, 97]
toArray() collects the IntStream values into an int[] array. This helps when code points need to be passed to another function, stored or analyzed repeatedly.
Converting code points back to characters
String tekstas = "A😀B";
tekstas.codePoints()
.mapToObj(kodas -> new String(Character.toChars(kodas)))
.forEach(System.out::println);A
😀
B
Character.toChars(kodas) converts a Unicode code point into one or two char units. Then new String(...) lets you print the full character, even if it does not fit in one char.
Filtering letters
String tekstas = "Java 17!";
long raidziuKiekis = tekstas.codePoints()
.filter(Character::isLetter)
.count();
System.out.println(raidziuKiekis);4
Character::isLetter checks whether a code point is a letter. Numbers, spaces and punctuation are excluded, so from Java 17! only four letters are counted.
Detecting supplementary Unicode characters
String tekstas = "Labas 😀";
boolean turiPapildomuSimboliu = tekstas.codePoints()
.anyMatch(kodas -> kodas > 0xFFFF);
System.out.println(turiPapildomuSimboliu);true
Code points greater than 0xFFFFdo not fit in one UTF-16 unit. Java strings store them as surrogate pairs, while codePoints() presents them as a single int value.
Empty text returns an empty stream
String tekstas = "";
System.out.println(tekstas.codePoints().count());0
A blank String contains no code points, so codePoints().count() returns 0. This is not an error.
A stream cannot be reused
import java.util.stream.IntStream;
String tekstas = "Java";
IntStream srautas = tekstas.codePoints();
System.out.println(srautas.count());
System.out.println(srautas.count());4
IllegalStateException
IntStream is single-use. Once a terminal operation, such as count(), consumes it, the same stream cannot be used again. To calculate again, call tekstas.codePoints() again.
Stream operations
String tekstas = "ABC";
int suma = tekstas.codePoints().sum();
System.out.println(suma);198
Because codePoints() returns IntStream, you can use standard numeric stream operations: sum(), count(), filter(), map(), anyMatch() and others.
Common mistakes
- Expecting
codePoints()to return an array when it returns anIntStream. - Trying to reuse a stream after a terminal operation.
length()is considered the same ascodePoints().count(), although emoji can change the result.- Trying to convert a code point directly to
char, although some characters requireCharacter.toChars(). - Forgetting that
count()returnslong, notint.
When should you use codePoints()?
codePoints() Use it to traverse text as Unicode code points: filter letters, count characters, detect emoji, transform text or use the Java Stream API. For one code point at a particular index, use codePointAt() or codePointBefore().