String charAt() method - Syntax and Examples
Learn how to use the Java String charAt() method. This tutorial covers syntax, parameters, return value and practical examples.
This reference explains String charAt() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
String charAt()
The charAt() method is used to access individual characters within a string by their index.
Syntax
public char charAt(int index)
Parameters
| Parameter | Description |
|---|---|
index |
The index of the character to retrieve. This is a zero-based index, meaning the first character is at index 0. |
Return Value
The charAt() method returns a char representing the character at the specified index in the string.
Examples
Example 1: Retrieving the First Character
This example demonstrates how to get the first character of a string using an index of 0.
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello";
char firstChar = str.charAt(0);
System.out.println("The first character is: " + firstChar);
}
}
The first character is: H
Here, we declare a string str as "Hello". Then, we call the charAt(0) method to retrieve the character at index 0 which is 'H'. Finally, it prints the retrieved character.
Example 2: Retrieving a Character from the Middle
This example shows how to access a specific character in the string by providing its index. Let's retrieve the character at index 2.
public class CharAtExample {
public static void main(String[] args) {
String str = "Java";
char middleChar = str.charAt(2);
System.out.println("The character at index 2 is: " + middleChar);
}
}
The character at index 2 is: v
In this example, we take the string 'Java' and access the element at index 2 which corresponds to the character 'v'. The method returns this value.
Example 3: Handling Index Out of Bounds
This demonstrates what happens when you try to access an invalid (out-of-bounds) index. Attempting to retrieve a character beyond the string's length will result in a StringIndexOutOfBoundsException.
public class CharAtExample {
public static void main(String[] args) {
String str = "Hello";
try {
char outOfBoundsChar = str.charAt(10); // Index 10 is out of bounds.
System.out.println("Character at index 10: " + outOfBoundsChar);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Index Out of Bounds - " + e.getMessage());
}
}
}
Error: Index Out of Bounds - String index out of range [0-4]
This example uses a try-catch block to handle the potential IndexOutOfBoundsException. Since we're trying to access an element at index 10 in "Hello" (which is only 5 characters long), Java throws this exception, which is then caught and displayed.
Example 4: Iterating Through a String
Here's how you can use `charAt()` to iterate through all the characters in a string.
public class CharAtExample {
public static void main(String[] args) {
String str = "World";
for (int i = 0; i < str.length(); i++) {
char currentChar = str.charAt(i);
System.out.println("Character at index " + i + ": " + currentChar);
}
}
}
Character at index 0: W
Character at index 1: o
Character at index 2: r
Character at index 3: l
Character at index 4: d
This loop iterates from index 0 to the last character (length - 1). In each iteration, it retrieves a character using charAt(i) and prints its value. This is a common technique when you need to process each individual character in a string.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.