java.lang.String codePointAt()

Description :

This java tutorial shows how to use the codePointAt() method of java.lang.String class. This method returns int data type which corresponds to the the Unicode point at specified index. This generally supports characters above the Charater.MAX_VALUE.

Method Syntax :

public int codePointAt(int index)

Parameter Input :

  • index – the index or position of the char value that we want to get.

Method Returns :

This method returns int datatype that corresponds to the code point value of the character at specified index.

Compatibility Version :

Requires Java 1.5 and up

Exception :

IndexOutOfBoundsException

– The codePointAt() method of String class throws IndexOutOfBoundsException whenever the index provided on the method argument is outside the range of the String. The maximum index argument is expected to be String length -1.

Java Code Example :

This example source code demonstrates the use of codePointAt() method of String class.

package com.javatutorialhq.tutorial;
/*
 * This source code shows the usage of the codePointAt() of String class
 */
public class StringCodePointAtDemo {

    public static void main(String[] args) {
        String input = "this is java tutorial";
        int index = 5;
        System.out.println("Output:"+input.codePointAt(index));
    }
}

Sample Output :

Running the codePointAt() example source code will give you the following output

java string codePointAt method example

java string codePointAt method example

Exception Scenario :

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 50
	at java.lang.String.codePointAt(Unknown Source)
	at com.teknoscope.tutorial.StringCodePointAtDemo.main(StringCodePointAtDemo.java:11)

Suggested Reading List :