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);97
- The variable
tekstasstores the value Java. codePointAt(1)reads the character at index1.- The index
1is the lettera. - The Unicode code point for the letter
ais97, so the method returns theintvalue97.
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)
| Part | Meaning |
|---|---|
| Calling object | tekstas in the line tekstas.codePointAt(1) |
| Parameter | index – the position in the String string where the code point is read |
| Return type | int |
| Valid indices | From 0 to length() - 1 |
| Error | IndexOutOfBoundsExceptionwhen 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);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));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);75
115
- The first index is
0, socodePointAt(0)retrieves the first code point. - The last valid index is
length() - 1. - The letter's
KUnicode code point is75. - The letter's
sUnicode code point is115.
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);
}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));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));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");
}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, not1. - Using this for the last element:
codePointAt(length())instead ofcodePointAt(length() - 1). codePointAt()is confused withcharAt(): the first returns anintcode point; the second returns achar.- 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().