VOOZH about

URL: https://www.geeksforgeeks.org/java/java-string-charat-method-example/

⇱ Java String charAt() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java String charAt() Method

Last Updated : 1 Jun, 2026

String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() - 1.

If the index value is greater than or equal to the string length or negative number it returns stringIndexOutOfBoundsException

Example 1:

Syntax of charAt() Method

public char charAt(int index)
  • Parameter: index - Index of the character to be returned.
  • Returns: Returns the character at the specified position.
  • Exceptions : StringIndexOutOfBoundsException - If the index is negative or greater than the length of the String.

Example 2: The following program demonstrating the Exception case as StringIndexOutOfBoundsException.

Example 3: Accessing the First and Last Character

Example 4: Print Characters Presented at Odd Positions and Even Positions


Example 5: Counting Frequency of a Character in a String

Comment