charAt() Method Syntax

Parameter Input for charAt() Method

index – This Java method accepts only single input which is an int data type.

Return Type of Java String charAt() Method

The Java String charAt() method returns a character type data based on the index input.

Exception

Throws java.lang.StringIndexOutOfBoundsException if index value is not between 0 and String length minus one

Example of Java String charAt() Method

Output: Character at 0 position is: T Character at 5th position is: i Character at 22nd position is: M Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1 Some important things about the Java charAt() method:

This Java method takes an argument which is always int type. This method returns the character as char for the given int argument. The int value specifies the index that starts at 0. If the index value is higher than string length or a negative, then IndexOutOfBounds Exception error occurs. The index range must be between 0 to string_length-1.