VOOZH about

URL: https://www.geeksforgeeks.org/java/java-string-codepointat-method/

⇱ Java String codePointAt() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java String codePointAt() Method

Last Updated : 23 Jul, 2025

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.


Output
Unicode value of character at index 1: 101

Syntax of codePointAt() Method

public int codePointAt(int index)

  • Parameter: The index of the character to retrieve the Unicode code point. It must be within the range 0 to string length - 1.
  • Return Type: Return an int value representing the Unicode value at the specified index.

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.


Output
Code point at index 10 : 129302

Example 3: If we attempt to access an invalid index, the codePointAt() method will throw an IndexOutOfBoundsException.

Output:

👁 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:


Output
Error: index 10,length 5
Comment
Article Tags:
Article Tags: