GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String charAt() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

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:

Java
class Geeks {
    public static void main(String args[]) {
      	
        // Define a string
        String s = "Java String charAt() example";

        // Retrieve and print the character at index 8
        char ch = s.charAt(8);
        System.out.println(ch);

        // Retrieve and print the character at index 24
        ch = s.charAt(24);
        System.out.println(ch);
    }
}

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.

Java
class Geeks {
    // main function
    public static void main(String args[])
    {
        String s = "abc";

        char ch = s.charAt(4);
        System.out.println(ch);
    }
}

Example 3: Accessing the First and Last Character

Java
import java.io.*;

class Geeks {
    // main function
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
        int len = str.length();

        // First Element
        System.out.println("First Element: "+ str.charAt(0));

        // Last Element
        System.out.println("First Element: "+ str.charAt(len - 1));
    }
}

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

Java
import java.io.*;

class Geeks {
 
    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";

        // Odd Positions
        System.out.println("Odd Positions");
        for (int i = 0; i < str.length(); i += 2) {
            System.out.print(str.charAt(i));
        }

        System.out.println("\n");

        // Even Positions
        System.out.println("Even Positions");
        for (int i = 1; i < str.length(); i += 2) {
            System.out.print(str.charAt(i));
        }
    }
}


Example 5: Counting Frequency of a Character in a String

Java
import java.io.*;

class Geeks {

    public static void main(String[] args)
    {
        String str = "GeeksforGeeks";
        int count = 0;

        for (int i = 0; i < str.length(); i++) {
          
            // Counting e in string
            if (str.charAt(i) == 'e')
                count++;
        }

        // printing occurrence
        System.out.println("Count the occurrence of e : "+ count);
    }
}
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.