What is the String.codePointAt() method in Java?

The codePointAt() method in Java is used to get the Unicode value of a character in the string at the specified index.

Syntax

The codePointAt() method can be declared as shown in the code snippet below:

public int codePointAt(int ind)

Parameters

  • ind: The index of the character whose Unicode value is required. The index value must be between 0 and the length of the string, which in this case is negative one.

Return value

The codePointAt() method returns an int, which is the Unicode value of the specified character.

The codePointAt() method throws the IndexOutOfBoundsException if:

  • The index ind is negative.
  • The index ind is greater than the length of the string.

Code

Consider the code snippet below, which demonstrates the use of the codePointAt() method.

class Main {
public static void main(String[] args)
{
String str = "Hello world!";
int code = str.codePointAt(3);
System.out.println("Code point at 3rd index: " + code);
}
}

Explanation

A string str is declared in line 5. The codePointAt() method is used in line 6 to get the Unicode value of the character at the third index in str.

Free Resources