While data handling in Java, there arise certain situations where there is a requirement to locate a required character based on its index or retrieve the corresponding index of a target character or substring instead. In such a scenario, the “charAt()” and “indexOf()” methods in Java come into effect which eases the searching process for the developer for update procedures.
This article will state the differences between the “charAt()” and “indexOf()” methods in Java.
What is the Difference Between charAt() and indexOf() in Java?
The “charAt()” method gives the character at the provided index within a string. On the other hand, the “indexOf()” method gives the index of the first occurrence of a specified character(s) or a substring in a string. The latter method can also be applied in a custom manner to start searching from the specified index.
Syntax (“charAt()” Method)
charAt(ind)
In this syntax, “ind” refers to the index of the character that needs to be returned.
Syntax (“indexOf()” Method)
indexOf(char, ind)
In the given syntax:
- “char” points to the index of the character or string to locate.
- “ind” is the index form where to start the search.
Example: Applying the “charAt()” and “indexOf()” Methods in Java
This example applies both the discussed methods on the initialized string to demonstrate their working:
public class Charatindex {
public static void main(String args[]){
String givenString = "This is Linuxhint";
System.out.println("Index of Character 's' is -> "+ givenString.indexOf("s"));
System.out.println("Index of Character 's' after the first occurence is -> "
+ givenString.indexOf("s", 4));
System.out.println("Index of substring (Linuxhint) is -> "+ givenString.indexOf("Linuxhint"));
System.out.println("The character at index 8 is -> "+givenString.charAt(8));
}}
According to these code lines:
- Initialize the provided string to be evaluated.
- Use the “indexOf()” method to retrieve the index of the given character i.e., ‘s’.
- Now, apply this method again by specifying the character to search and the starting index from where to start the search for this character.
- It is such that the search will be started from the index “4”.
- In the third scenario, the “indexOf()” method logs the index of the stated substring.
- After that, implement the “charAt()” method to retrieve the character against the specified index i.e., “8”.
Output

In this output, the differences between both methods can be analyzed.
It is such that the “indexOf()” method returns the index against the specified character, the index of the same character based on the starting search index, and the index of a substring, respectively. On the other hand, the “charAt()” method logs the character at the corresponding index instead.
Conclusion
In Java, the “charAt()” method returns the character at the particular index within a string whereas, the “indexOf()” method gives the index of the first occurrence of the specified character, or a substring in a string. This blog discussed both the distinct methods and their differences in Java.