The string class in Java implements certain methods and functions using the strings. The charAt() method is one of the most popularly used built-in methods in Java. In this method, the index number is provided as input and the output appears as the character from the string on that specified index location. If there is no character available at the specified index, StringIndexOutOfBoundsException occurs.
In this write-up, we will put into words the implementation of the charAt() method of String in Java.
How to Use String charAt() Method in Java?
To use the charAt() method in Java, all you have to do is pass an index number as input to the charAt() method. Consequently, the stated function will retrieve the character available at that particular index.
Let us look at the syntax of the charAt() method of Java that takes input as a parameter.
public char charAt(int index)
Now we will proceed with the code implementation of the charAt() method to get more clarity.
Example 1: Basic Implementation of charAt() Method in Java
The below code describes the implementation of the charAt() method of Java.
//Declare class to implement the charAt() method
class CharacterMethod {
public static void main(String args[])
{
//declare a string to get the character at the specified index
String str = "Working with strings in Java";
char c = str.charAt(4);
System.out.println("The character is " + c);
c = str.charAt(30);
System.out.println("The character is " + c);
}
}
In the above Java code:
- A class is declared for the charAt() Method.
- The string is declared as “str” in the next step.
- The specific characters at a given index number are obtained using the charAt() method.
- The last index number is declared as 30, which gives the error since there are no characters at that specified index.
Output
The below output depicts that the character “i” is present at index number 4 and the character “g” is present at index number 6. An error of StringIndexOutOfBoundsException occurs since there are no characters at the specified index.

Example 2: To Fetch the First and the Last Element Using charAt() Method
The charAt() method is also used to fetch the first and the last element of the String. The first and the last characters are fetched using the charAt() method.
//Import the required packages
import java.io.*;
//Declare a class
class CharacterMethod2 {
public static void main(String[] args)
{
String s = "ProgrammminginPython";
int ln = s.length();
//To get the first element
System.out.println("The First Element is : "+ s.charAt(0));
//To get the last element
System.out.println("The Last Element is : "+ s.charAt(ln - 1));
}
}
In the above-described code:
- The required packages and the class are imported and declared respectively to get the first and the last element.
- The string is declared as “s” and the length object is declared as “ln”.
- The first index is at “0” and the last index(ln -1) is printed using the println() method.
Output
The below output shows that the first character is printed as “P” and “n” as the last character.

Example 3: To Count the Frequency of Characters
The term “frequency” refers to the number of times a character appeared in a specified string. The code below implements the integer “counts” that calculates the frequency using the charAt() method.
//Import the required packages
import java.io.*;
//declare a class to implement the frequency count
class CharacterMethod3 {
public static void main(String[] args)
{
//Declare the string
String s = "ProgrammminginPython";
int counts=0;
for (int x= 0; x <s.length(); x++) {
//Declare the character to get the number of times it is present in the string
if (s.charAt(x) == 'n')
counts++;
}
System.out.println("The character n has appeared : " + counts);
}
}
In the above code block, the if statement checks for the character “n” using the charAt() method, and the output is displayed using the println() method.
Output
The output below depicts that the character “n” appears 3 times in the declared string.

Conclusion
The string charAt() method returns the specified character according to the declared index. This method is used to fetch the first and last element and to calculate the frequency of characters in a string. In this article, we have implemented the charAt() method in detail using different examples.