Javatpoint Logo
Javatpoint Logo

Java String charAt()

The java string charAt() method returns a char value at the given index number.

The index number starts from 0 and goes to n-1, where n is length of the string. It returns StringIndexOutOfBoundsException if given index number is greater than or equal to this string length or a negative number.

Internal implementation


The signature of string charAt() method is given below:


index : index number, starts with 0


A char value

Specified by

CharSequence interface, located inside java.lang package.


StringIndexOutOfBoundsException : if index is negative value or greater than this string length.

Java String charAt() method example

Test it Now



StringIndexOutOfBoundsException with charAt()

Let's see the example of charAt() method where we are passing greater index value. In such case, it throws StringIndexOutOfBoundsException at run time.


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: 10
at java.lang.String.charAt(
at CharAtExample.main(

Java String charAt() Example 3

Let's see a simple example where we are accessing first and last character from the provided string.


Character at 0 index is: W
Character at last index is: l

Java String charAt() Example 4

Let's see an example where we are accessing all the elements present at odd index.


Char at 1 place e
Char at 3 place c
Char at 5 place m
Char at 7 place  
Char at 9 place o
Char at 11 place J
Char at 13 place v
Char at 15 place t
Char at 17 place o
Char at 19 place n
Char at 21 place  
Char at 23 place o
Char at 25 place t
Char at 27 place l

Java String charAt() Example 5

Let's see an example where we are counting frequency of a character in the string.


Frequency of t is: 4

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA