Java codePointAt Method

The Java codePointAt method is one of the Java String Methods, which is to return the Unicode of the character at the specified index position. In this article, we will show how to use the codePointAt method in Java Programming language with example. The index position of the Java code Point At Function will start from 0, Not 1.

Java codePointAt method syntax

The basic syntax of the codePointAt in Java Programming language is as shown below.

public int codePointAt(int Index_Position) 

//In order to use in program
String_Object.codePointAt(int Index_Position)
  • String_Object: Please specify the valid String Object.
  • Index_Position: Please specify the index position of the desired character.

The Java codePointAt Function will return the Unicode value of a Character from String_Object at specified Index_Position. If we specify the Index position out of range or negative value, then the codePointAt function will throw an error.

Java codePointAt Example

The Java string codePointAt method returns the Unicode value of a Character at the specified index position. In this Java program, We are going to find the same.

package StringFunctions;

public class CodePointAt {
	public static void main(String[] args) {
		String str = "Learn Free Java Tutorial";
		
		int a = str.codePointAt(0);
		int b = str.codePointAt(8);
		int c = str.codePointAt(14);
		int d = str.codePointAt(str.length()- 1);
		
		System.out.println( "Unicode Vlaue of Character at Index position 0 = " + a);
		System.out.println( "Unicode Vlaue of Character at Index position 8 = " + b);
		System.out.println( "Unicode Vlaue of Character at Index position 14 = " + c);
		System.out.println( "Unicode Vlaue of Character at Last Index position = " + d);
	}
}
Java codePointAt Method 1

Within this Java codePointAt method example, we declared the String variable and assigned a value using the following statement.

String str = "Learn Free Java Tutorial";

The following Java statements will find the Character at index positions 0, 8, 14. Then assign the Unicode values of those characters to the integer variables a, b, and c.

int a = str.codePointAt(0);
int b = str.codePointAt(8);
int c = str.codePointAt(14);

If you observe the above screenshot, str.codePointAt(0) is returning 76. It is because we all know that character at index position 0 is L, and according to ASCII Table, the Unicode value of L is 76. You should count the space as One Character.

In the next line, we are using the Java length function to calculate the string length.

int d = str.codePointAt(str.length()- 1);

From the above String Method code snippet, we are subtracting one from string length because the length of a string will count from 1 to n, whereas the index position will start from 0 and end’s at n – 1. The following System.out.println statements will print the output

System.out.println( "Unicode Vlaue of Character at Index position 0 = " + a);
System.out.println( "Unicode Vlaue of Character at Index position 8 = " + b);
System.out.println( "Unicode Vlaue of Character at Index position 14 = " + c);
System.out.println( "Unicode Vlaue of Character at Last Index position = " + d);