STRING INSTANCE METHODS

Java String charAt() method

charAt() returns one char character from a String string at the specified index. Java string indices start at 0.

Retrieving your first character

String zodis = "Java";
char raide = zodis.charAt(1);

System.out.println(raide);
Console output

a

  1. The variable zodis stores the text Java.
  2. charAt(1) requests the character at index 1.
  3. The index 0 is the letter J, so index 1 is the first a.
  4. The method returns char, not String.

Why is charAt() an instance method?

charAt() is called on a specific string because it reads a character from that string:

char raide = zodis.charAt(1);

zodis is the object whose character is read. We therefore do not write String.charAt(...); the method needs a specific String value.

Method syntax

public char charAt(int index)

Parts of the charAt() method
PartMeaning
Calling objectzodis in the line zodis.charAt(1)
Parameterindex – the character's position in the string
Return typechar
Valid indicesFrom 0 to length() - 1
ErrorStringIndexOutOfBoundsExceptionwhen the index is outside the string's bounds

Indices start at zero

String zodis = "Java";

System.out.println(zodis.charAt(0));
System.out.println(zodis.charAt(1));
System.out.println(zodis.charAt(2));
System.out.println(zodis.charAt(3));
Console output

J
a
v
a

The text Java has four characters, but its last index is 3, not 4. This is because the first character has index 0.

First and last character

String tekstas = "Programavimas";

char pirma = tekstas.charAt(0);
char paskutine = tekstas.charAt(tekstas.length() - 1);

System.out.println(pirma);
System.out.println(paskutine);
Console output

P
s

  1. The first character is always at index 0if the string is not empty.
  2. length() returns the character count.
  3. The last character's index is one less than the length: length() - 1.
  4. This principle is often used to check endings, trailing symbols or the final letter of input.

Traversing text with a loop

String tekstas = "kodas";

for (int i = 0; i < tekstas.length(); i++) {
  System.out.println(i + ": " + tekstas.charAt(i));
}
Console output

0: k
1: o
2: d
3: a
4: s

for loop starts at 0 and continues while i is less than tekstas.length(). This condition prevents going beyond the last character.

Counting a character in text

String tekstas = "programavimas";
int kiekis = 0;

for (int i = 0; i < tekstas.length(); i++) {
  if (tekstas.charAt(i) == 'a') {
    kiekis++;
  }
}

System.out.println(kiekis);
Console output

3

Here, charAt(i) retrieves one character on each iteration. If it equals 'a', the counter increases. Notice that a char literal uses single quotes.

An index cannot equal length()

String zodis = "Java";

System.out.println(zodis.charAt(4));
Result

StringIndexOutOfBoundsException

Java has length 4, but its valid indices are 0, 1, 2 and 3. Index 4 is already outside the string.

Negative indices are invalid too

String zodis = "Java";

System.out.println(zodis.charAt(-1));
Result

StringIndexOutOfBoundsException

Java String indices do not count from the end, so -1 does not mean the last character. Retrieve the last character using tekstas.charAt(tekstas.length() - 1).

Empty text has no characters

String tekstas = "";

System.out.println(tekstas.charAt(0));
Result

StringIndexOutOfBoundsException

An empty string has length 0, so it has no valid index. Before retrieving the first or last character, check !tekstas.isEmpty().

Common mistakes

  • Forgetting that the first index is 0, not 1.
  • Using this for the last character: charAt(length()) instead of charAt(length() - 1).
  • Trying to call charAt() with a negative index.
  • Calling the method on an empty string without checking isEmpty().
  • Confusing char and String: charAt() returns one character, not a string.

When should you use charAt()?

charAt() Use it to retrieve or check a specific character: the first letter, final symbol, a user-selected position or each character in a loop. For a substring of several characters, a better choice is substring().