In data handling, there can be situations in Java where there is a need to extract a character from the string. More specifically, in the case of strings comprising multiple substrings or updating the data via removal of a target character. In such situations, the Java “charAt()” method does wonders in returning the required character via indexing.
This tutorial will explain applying the String “charAt()” method in Java.
What is the Java String charAt() Method?
The “charAt()” method retrieves the character at the target index in a string.
Syntax
charAt(in)
In this syntax, “in” corresponds to the index of the character to be returned.
Return Value
It retrieves the character at the particular index.
Note: If the index passed to the “charAt()” method is negative or out of range, it throws the “IndexOutOfBoundsException” exception.
Example 1: Applying the Java String “charAt()” Method
This example demonstrates the working of the “charAt()” method:
public class Charat {
public static void main(String args[]){
String givenString = "This is Java Programming Language!";
System.out.println("The character at index 3 is -> "+givenString.charAt(3));
}}
In this code snippet:
- Initialize the given string from which the target character needs to be returned.
- Now, associate the “charAt()” method with the string having the stated index that points to the string character against that index to be retrieved.
Output
This outcome implies that the corresponding string character is returned accordingly.
Example 2: Returning the Exception Thrown by the “charAt()” Method
In this demonstration, an exception will be thrown by specifying an out-of-range index:
public class Charat {
public static void main(String args[]){
String givenString = "This is Java Programming Language!";
System.out.println("The character at index 3 is -> "+givenString.charAt(40));
}}
In these code lines, likewise, define a string and apply the “charAt()” method. Here, the specified index is not contained in the string (out of range). Therefore, an “IndexOutOfBoundsExcpetion” will be thrown.
Output
As seen, the discussed exception is thrown indicating that the specified index is not contained in the string.
Example 3: Resolving the Exception Thrown by the Method
In this example, the faced exception can be resolved:
public class Charat {
public static void main(String args[]){
String givenString = "This is Java Programming Language!";
try {
System.out.println("The character at index 3 is -> "+givenString.charAt(40));
}
catch (IndexOutOfBoundsException exc) {
System.out.println("Exception Handled!");
}
}}
According to this block of code:
- Similarly, define the provided string.
- Now, include a “try” block comprising the applied “charAt()” method pointing to the non-existing index in the string i.e., 40.
- After that, in the “catch” block, specify the probable exception i.e., “IndexOutOfBoundsException” and include the stated message.
Output

In this output, it can be analyzed that the discussed exception is handled appropriately via the displayed corresponding message in the “catch” block.
Conclusion
The “charAt()” method retrieves the character at the target index in a string and throws an “IndexOutOfBoundsException” if the index is not found in the string. This tutorial demonstrated the working of the Java “charAt()” method.