Search⌘ K
AI Features

Strings Indexing, Length, and Traversal

Explore Java string fundamentals by learning how to access characters using zero-based indexing, measure string length with the length() method, and traverse strings systematically. This lesson helps you master key operations essential for string manipulation and algorithm development, including forward and reverse iteration styles and handling index boundaries safely.

By now, you know that a string is an ordered sequence of characters stored contiguously in memory and that its structure is essentially identical to an array. It is a clean and simple idea, but an idea alone does not help you solve problems. The real question is how you actually work with a string once you have one. How do you reach into it and pull out a specific character? How do you know how many characters it contains? How do you move through it one step at a time? These are the basic operations that every string algorithm depends on, and getting comfortable with them is what turns the conceptual picture of strings into something you can actually use.

Indexing in strings

Each character in the string is associated with a specific position called an index. Indexing starts at zero, meaning the first character is at index 0, the second is at index 1, the third is at index 2, and so on. This is the same zero-based indexing that arrays use.

Accessing individual characters

Because a string is stored like an array, accessing an individual character works exactly the same way as accessing an element in an array. To retrieve a character at a specific index, call the charAt() method on the string with the index as the argument.

Java 25
public class Main {
public static void main(String[] args) {
String s = "hello";
System.out.println(s.charAt(0)); // h
System.out.println(s.charAt(1)); // e
System.out.println(s.charAt(2)); // l
System.out.println(s.charAt(3)); // l
System.out.println(s.charAt(4)); // o
}
}

The result of indexing into a string is always a single character. This operation takes constant time O(1)O(1) regardless of which index you use because the computer can calculate the exact memory address of any character directly from its index, just as it does with arrays.

Access a character from the end of the string

Java does not support negative indexing directly. To access a character from the end of a string, you calculate the index using the string's length:

Java 25
public class Main {
public static void main(String[] args) {
String s = "hello";
System.out.println(s.charAt(s.length() - 1)); // o (equivalent to s[-1])
System.out.println(s.charAt(s.length() - 2)); // l (equivalent to s[-2])
System.out.println(s.charAt(s.length() - 3)); // l (equivalent to s[-3])
System.out.println(s.charAt(s.length() - 4)); // e (equivalent to s[-4])
System.out.println(s.charAt(s.length() - 5)); // h (equivalent to s[-5])
}
}

While Java does not offer negative indexing as a built-in shortcut, the pattern s.charAt(s.length() - 1) is the standard Java equivalent of s[-1] in Python. Many string problems require looking at the last character or working backward from the end, and this pattern makes that straightforward. The length() method returns the total number of characters in a string, which is covered properly in later sections.

It helps to think of forward and backward access as two different ways of referring to the same positions in a string. For a string of length n, the character at positive index i is the same as the character at index s.length() - (n - i) counted from the end.

String: h e l l o
Positive: 0 1 2 3 4
Negative: -5 -4 -3 -2 -1

Both rows describe the same five positions. Python simply gives you a choice of which direction to count from.

Now, take a look at the following visualizer to better understand ...