Last Updated: Jun 15, 2025
Easy

String charAt() in Java

Author Aditi
0 upvote
Table of contents
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn more about the java string charAt() function with the help of examples. The charAt() method of the Java String class returns a char value at the specified index number. The index number starts at 0 and continues up to n-1, where n is the string length. If the specified index number is larger than or equal to this string length or a negative integer, it throws StringIndexOutOfBoundsException.

String charAt() in Java

What is charAt() in Java?

charAt() in Java is a method of the String class used to retrieve the character at a specific index within a string. It is called using the syntax string.charAt(index). The index starts from 0, and the method returns the character present at the specified position. If the index is out of range, it throws a StringIndexOutOfBoundsException.

Syntax:

public char charAt(int index) 

The index argument is accepted by the procedure. The initial index is zero. It returns a character from a string at a given index position. If the index is negative or longer than this string length, it raises StringIndexOutOfBoundsException.

Example of Java String charAt()

Example 1:

The charAt() function returns the character in a string at the given index. The first character has an index of 0, and the second has an index of 1, and so on.

Program:

public class Stringoperation4  
{  
    public static void main(String args[])  
    {  
        String str="CodingNinjas";    
        System.out.println(str.charAt(0));//C    
        System.out.println(str.charAt(6));//N    
    }  
}
You can also try this code with Online Java Compiler
Run Code

Output:

C
N

Let's look at using the charAt() function with a higher index value. At runtime, it throws a StringIndexOutOfBoundsException in this situation.

Example 2:

Program:

public class charExample{  
    public static void main(String args[]){  
        String str="codingninjas";  
        char ch=str.charAt(20);//returns char at the 10th index  
        System.out.println(ch);  
    }
}  
You can also try this code with Online Java Compiler
Run Code

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 20
at java.lang.String.charAt(String.java:658)
at charExample.main(charExample.java:4)

Frequently Asked Questions

How to use char() in Java?

Java does not have a char() method. Instead, use the char data type directly or access characters using the charAt() method.

What is the return type of charAt?

The return type of the charAt() method in Java is char, which represents a single 16-bit Unicode character from the string.

How to check each character in a string in Java?

Use a for loop with the charAt() method to access and check each character in the string one by one.

Example:

for (int i = 0; i < str.length(); i++) {  
   char c = str.charAt(i);  
   // check c  
}

What is a string class in Java?

The java.lang.String class in Java has a multitude of built-in methods for manipulating strings. We may execute actions on String objects using these methods, such as cutting, concatenating, converting, comparing, and replacing strings.

What do you mean by strings being immutable?

String objects are immutable in Java, meaning they can't be updated or modified. A string object's data or state cannot be changed after being created.

Which type of indexing is used in the charAt() method?

0-based indexing is used while using the charAt() method. The first character has an index of 0; the second has an index of 1, and so on.

Conclusion

The charAt() method in Java is a simple yet powerful tool for accessing individual characters in a string based on their index. It is widely used in string manipulation, validation, and character-based operations. Understanding how to use charAt() effectively helps in writing efficient code for tasks like iteration, comparison, and searching within strings.

Recommended Readings: