Signup/Sign In

Java Character codePointAt(char[] a, int index) Method

Java codePointAt() method is a part of Character class. This method returns the codepoint at the index specified of the char array. If at a particular index, the char value at a given index is in the low surrogate range then the codepoint corresponding to this pair will be returned else if the char value in the char array is in the high surrogate range, the following index is less than the index of char array is returned.

Syntax:

public static int codePointAt(char[] a, int index)  

Parameter:

The parameter passed are:

  • a char array

  • int index to the char value in the char array.

Returns:

Returns the codepoint value of the character at the passed index.

Example 1:

Here, the codepoint value of the character at the specified index is returned.

import java.lang.Character;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        char[] ch = new char[] { 'm', 'o', 'h', 'i', 't' };
        int index = 4;  
        
        int r = Character.codePointAt(ch,index);
        System.out.println("The codepoint value is : "+r);
    }  
}


The codepoint value is : 116

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the desired output.

import java.lang.Character;
import java.util.Scanner;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        try
        {
          System.out.println("Enter the characters");
          Scanner sc = new Scanner(System.in);
          char[] ch = sc.next().toCharArray();
          System.out.println("Enter index");
          int n = sc.nextInt();
          System.out.println("The Codepoint value is : "+Character.codePointAt(ch,n));       
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input");
        }
    }  
}


Enter the characters
studytonight
Enter index
2
The Codepoint value is : 117
************************************
Enter the characters
happy
Enter index
4
The Codepoint value is : 121

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.