![]() |
VOOZH | about |
The codePointAt() method in Java is part of the String class. It is used to return the Unicode value of the character at the specified index in the string. This method is very useful when working with characters beyond the Basic Multilingual Plane (BMP), such as emojis or special symbols.
Example 1: The below Java program demonstrates how to retrieve the Unicode value of a character at the specified index.
Unicode value of character at index 1: 101
public int codePointAt(int index)
0 to string length - 1.Example 2: The below Java program demonstrates how to retrieve the Unicode value of a character including an emoji, at a specified index in a string.
Code point at index 10 : 129302
Example 3: If we attempt to access an invalid index, the codePointAt() method will throw an IndexOutOfBoundsException.
Output:
Note: The exception occurs because the index 10 is beyond the string length (which is 5 in this case).
Example 4: To handle the exception correctly, we can use a try-catch block as shown in the below example:
Error: index 10,length 5