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);
Console output

74
97
118
97

  1. The variable tekstas stores the value Java.
  2. codePoints() creates an IntStream stream.
  3. Each stream element is one Unicode code point.
  4. 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()

Parts of the codePoints() method
PartMeaning
Calling objecttekstas in the line tekstas.codePoints()
ParametersNo parameters
Return typeIntStream
Stream valuesint Unicode code points
Surrogate pairsCorrectly combined into one code point
Empty stringReturns an empty stream

The difference from length()

String tekstas = "A😀B";

System.out.println(tekstas.length());
System.out.println(tekstas.codePoints().count());
Console output

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));
Console output

[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);
Console output

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);
Console output

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);
Console output

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());
Console output

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());
Result

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);
Console output

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 an IntStream.
  • Trying to reuse a stream after a terminal operation.
  • length() is considered the same as codePoints().count(), although emoji can change the result.
  • Trying to convert a code point directly to char , although some characters require Character.toChars().
  • Forgetting that count() returns long, not int.

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().