STRING INSTANCE METHODS

Java String codePointAt() method

codePointAt() returns the Unicode code point at the specified String index. It is useful when text contains more than basic letters, including characters beyond a single char , such as emoji.

Retrieving your first code point

String tekstas = "Java";
int kodas = tekstas.codePointAt(1);

System.out.println(kodas);
Console output

97

  1. The variable tekstas stores the value Java.
  2. codePointAt(1) reads the character at index 1.
  3. The index 1 is the letter a.
  4. The Unicode code point for the letter a is 97, so the method returns the int value 97.

Why is codePointAt() an instance method?

codePointAt() is called on a specific string because it reads a code point from that string:

int kodas = tekstas.codePointAt(1);

We therefore do not write String.codePointAt(...). The method needs a specific String object and the index where reading starts.

Method syntax

public int codePointAt(int index)

Parts of the codePointAt() method
PartMeaning
Calling objecttekstas in the line tekstas.codePointAt(1)
Parameterindex – the position in the String string where the code point is read
Return typeint
Valid indicesFrom 0 to length() - 1
ErrorIndexOutOfBoundsExceptionwhen the index is negative or greater than or equal to length()

The difference from charAt()

String tekstas = "A";

char simbolis = tekstas.charAt(0);
int kodas = tekstas.codePointAt(0);

System.out.println(simbolis);
System.out.println(kodas);
Console output

A
65

charAt() returns char value, while codePointAt() returns a numeric Unicode code point. For basic letters they appear similar, but serve different purposes: one returns a character unit, the other a character code.

Emoji and supplementary Unicode characters

String tekstas = "A😀B";

System.out.println(tekstas.length());
System.out.println(tekstas.codePointAt(1));
Console output

4
128512

The text A😀B looks like three characters to a person, but Java's String.length() returns 4because the emoji uses two UTF-16 code units. When codePointAt(1) points to the emoji's start, it returns the complete Unicode code point, 128512.

First and last code point

String tekstas = "Kodas";

int pirmas = tekstas.codePointAt(0);
int paskutinis = tekstas.codePointAt(tekstas.length() - 1);

System.out.println(pirmas);
System.out.println(paskutinis);
Console output

75
115

  1. The first index is 0, so codePointAt(0) retrieves the first code point.
  2. The last valid index is length() - 1.
  3. The letter's K Unicode code point is 75.
  4. The letter's s Unicode code point is 115.

Traversing code points safely

String tekstas = "A😀B";

for (int i = 0; i < tekstas.length();) {
  int kodas = tekstas.codePointAt(i);
  System.out.println(kodas);
  i += Character.charCount(kodas);
}
Console output

65
128512
66

If the text may contain emoji or other supplementary Unicode characters, a simple i++ can move into the middle of a character. Character.charCount(kodas) tells you whether a code point uses one or two UTF-16 units, so the loop advances correctly.

An index cannot equal length()

String tekstas = "Java";

System.out.println(tekstas.codePointAt(4));
Result

StringIndexOutOfBoundsException

Java has length 4, but its valid indices are 0, 1, 2 and 3. Index 4 is already outside the string, so Java throws StringIndexOutOfBoundsException, a kind of IndexOutOfBoundsException exception.

A negative index is invalid

String tekstas = "Java";

System.out.println(tekstas.codePointAt(-1));
Result

StringIndexOutOfBoundsException

Java String indices do not count from the end. -1 does not mean the last character, so before calling codePointAt() , validate the index.

Handling an error

String tekstas = "Java";
int indeksas = 10;

try {
  System.out.println(tekstas.codePointAt(indeksas));
} catch (IndexOutOfBoundsException e) {
  System.out.println("Netinkamas indeksas");
}
Console output

Netinkamas indeksas

In practice, checking bounds beforehand is better than catching an error: indeksas >= 0 && indeksas < tekstas.length(). try-catch is useful when the index comes from an external source and you need to show a clear error to the user.

Common mistakes

  • Forgetting that the first index is 0, not 1.
  • Using this for the last element: codePointAt(length()) instead of codePointAt(length() - 1).
  • codePointAt() is confused with charAt(): the first returns an int code point; the second returns a char.
  • Using this in a loop: i++, even though the text may contain emoji or other characters requiring two UTF-16 units.
  • Calling the method with a negative index or on an empty string.

When should you use codePointAt()?

codePointAt() Use it for Unicode code points: checking character codes, processing emoji, validating Unicode ranges or writing logic where char may be too limited. For a simple Latin letter at an index, this is often enough: charAt().