Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

String.codePointAt — code point at a UTF-16 index

String.codePointAt returns an int representing a code point, but accepts a UTF-16 code-unit index, not a code-point ordinal.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public int codePointAt(int index)

String.codePointAt(int index) reads at a UTF-16 code-unit (char) index. Its result is an int, unlike the result of charAt.

If the selected unit is a high surrogate followed by a low surrogate, the method combines the pair. Otherwise, it returns the selected char value, including an unpaired surrogate. Selecting a low surrogate does not scan backward to the pair's start.

The requirement is 0 <= index && index < text.length(). Violations throw IndexOutOfBoundsException; an empty string has no valid index.

In the example, the pair \uD83D\uDE00 occupies two indices: 1 yields 128512, whereas 2 yields the low-surrogate value 56832.

Examples

public class Main {
    public static void main(String[] args) {
        String text = "A\uD83D\uDE00B";
        System.out.println(text.length());
        System.out.println(text.codePointAt(0));
        System.out.println(text.codePointAt(1));
        System.out.println(text.codePointAt(2));
    }
}

Result

4
65
128512
56832

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources