Java codePointAt

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

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 codePointAt Function will return the Unicode value of a Character from String_Object at the specified Index_Position. If we specify the Index position out of range or negative value, the codePointAt function will throw an error.

Java codePointAt Example

The 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 Example

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

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

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 the character at index position 0 is L, and according to the ASCII Table, the Unicode value of L is 76. You should count the space as One Character.

In the next line, we use 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 the string length because the length of a string will count from 1 to n, whereas the index position will start from 0 and end at n – 1. The following four System.out.println statements will print the output.

TIP: The next step is to learn about the Java format and charAt functions from the Java String Methods.

What does Java codePointAt return if the index is out of bounds?

The built-in String codePointAt function accepts a position index as an argument, and it should be within the valid range. When we pass a negative index value or a positive integer greater than the string length, the codePointAt() method returns an IndexOutOfBoundsException.

public class example {
public static void main(String[] args)
{
String food = "Burger";
System.out.println(food.codePointAt(10));
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 6

How to iterate over all code points in a Java String?

We can use a for loop or a while loop to iterate over the characters in a given string. Inside the loop, use the codePointAt function to return the Unicode code point of each character.

public class example {
public static void main(String[] args)
{
String food = "Burger";

for(int i = 0; i < food.length(); i++)
{
System.out.println(food.codePointAt(i));
}
}
}
66
117
114
103
101
114