0Pricing
Java Academy · Lesson

Looping Over Characters

Process each char in a string.

Strings Are Made of Chars

A String is a sequence of characters. Each character sits at a position called an index, starting at 0.

In "Java", index 0 is 'J', index 1 is 'a', and so on. Looping lets you visit each char in order.

String word = "Java";
// indexes: 0=J 1=a 2=v 3=a

charAt Gets One Character

The charAt(index) method returns the char at a given position in a String.

Indexes run from 0 to length() - 1. Asking for an index outside that range throws an error.

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        System.out.println(word.charAt(0));
        System.out.println(word.charAt(3));
    }
}

All lessons in this course

  1. The char Primitive
  2. Character Helper Methods
  3. Looping Over Characters
  4. Counting and Filtering Chars
← Back to Java Academy