Java Strings & Methods Explorer
Explore Java String methods interactively. Select any method, adjust parameters, and see the result highlighted character by character on an SVG diagram. Includes string immutability demos, == vs .equals() comparison, method chaining, and AP CSA code tracing problems.
Controls
Java Code
1String str = "Hello World";2int len = str.length();3// len = 11
Result
The string has 11 characters.
Reference Guide
String Methods Quick Reference
length() returns the number of characters. charAt(i) returns the character at index i (0-based). Both are O(1) operations.
substring(begin) returns from begin to end. substring(begin, end) returns from begin (inclusive) to end (exclusive). A common AP CSA pitfall is forgetting that end is exclusive.
indexOf(str) returns the first position of str, or -1 if not found. indexOf(str, from) starts searching at from. lastIndexOf(str) finds the last occurrence.
toUpperCase(), toLowerCase(), trim(), replace(old, new), and concat(str) all return new strings. The original string is never modified.
String Immutability
Java Strings are immutable, meaning once created, their characters cannot change. Every method that appears to modify a string actually creates and returns a new String object.
Writing str.toUpperCase() without assigning the result is a common bug. The uppercase version is created and immediately discarded. You need str = str.toUpperCase() to keep it.
String concatenation with + also creates new objects. In a loop, this produces O(n) intermediate strings. For heavy concatenation, Java provides StringBuilder (not on the AP CSA exam).
Immutability makes strings safe to share between variables. If two variables point to the same string, neither can corrupt the other's data.
== vs .equals()
The == operator compares references (memory addresses), not content. Two variables that point to different String objects with identical characters will return false with ==.
The .equals() method compares content. It checks whether two strings contain the same sequence of characters, regardless of where they are stored in memory.
Java maintains a String pool for literal strings. When you write "Hello" twice in your code, both point to the same pool object, so == happens to return true. But new String("Hello") bypasses the pool and creates a separate heap object.
AP CSA Rule: always use .equals() for String comparison. Never rely on == for comparing string content.
substring & indexOf Patterns
The most common AP CSA pattern combines indexOf and substring to extract parts of a string. For example, extracting the username from an email: email.substring(0, email.indexOf("@")).
substring(begin, end) uses half-open interval notation: begin is included, end is excluded. The length of the result is always end - begin.
indexOf returns -1 when the target is not found. Always check for -1 before using the result with substring, or you risk a StringIndexOutOfBoundsException.
Method chaining works because each method returns a value. str.trim().toLowerCase().substring(0, 5) first trims, then lowercases the trimmed result, then takes the first 5 characters of that.