In AP Computer Science A, indexOf(String str) is a String method on the Java Quick Reference that returns the index of the first occurrence of str inside the calling string, or -1 if str does not appear. It searches and locates; it never changes the original string.
indexOf is one of the String methods you're guaranteed to have on exam day because it's printed on the Java Quick Reference. Calling text.indexOf("put") scans text from left to right and returns the index where "put" first begins. If the substring isn't there at all, it returns -1. That -1 is not an error and not an exception. It's a normal return value that your code checks with an if statement, which is exactly how the AP exam likes to test it.
Two details trip people up. First, indexOf only finds the first occurrence, so "banana".indexOf("an") is 1, not 3. Second, like every String method, indexOf doesn't modify anything (EK 1.15.A.3 says String objects are immutable). Think of indexOf as asking "where does this live?" and substring as "now go get it." On the exam they almost always travel together, with indexOf finding a position and substring using that position to slice the string.
indexOf lives in Topic 1.15 (String Methods) in Unit 1: Using Objects and Methods, supporting learning objective AP Comp Sci A 1.15.B, which asks you to call methods on String objects and determine the result. EK 1.15.B.2 lists indexOf as one of the required String methods on the Java Quick Reference, alongside length, substring, equals, and compareTo. It also connects to EK 1.15.B.1 on valid index ranges, since the int that indexOf hands back usually gets fed straight into substring, where an off-by-one mistake causes a StringIndexOutOfBoundsException. String processing built on indexOf is one of the most reliable FRQ patterns in the whole course, showing up in released questions across multiple years.
Keep studying AP® Computer Science A Unit 1
substring method (Unit 1)
These two are a matched set. indexOf tells you where something is, and substring uses that index to pull text out. The classic exam move is text.substring(0, pos) + text.substring(pos + 3) to delete a 3-character match, and you need indexOf to find pos first.
charAt method (Unit 1)
charAt is indexOf running in reverse. You give charAt an index and get back a character; you give indexOf a substring and get back an index. Mixing up which direction each one goes is one of the most common Unit 1 MCQ traps.
String immutability (Unit 1)
Per EK 1.15.A.3, indexOf never changes the string it's called on. Any "modify the string" task actually means building a new string with concatenation and substring, using indexOf to find where the change goes.
String traversal with loops (Unit 4)
Once loops arrive, indexOf gets upgraded. To find every occurrence instead of just the first, you wrap indexOf in a while loop and keep searching the rest of the string, which is exactly the structure behind FRQs like 2017's Phrase question.
On multiple choice, expect code-tracing stems like "What is printed as a result of executing this code segment?" where indexOf finds a position and substring slices around it. You'll also see the -1 sentinel pattern, where code checks if (input.indexOf("@") == -1) and branches differently depending on whether the substring exists. Practice questions love email-style strings for exactly this reason.
On the free response, indexOf is a workhorse. The 2017 FRQ (Phrase class) had you analyze and modify a string, the 2021 FRQ (WordMatch) had you compare strings against a secret string, and the 2024 FRQ (WordChecker) analyzed a list of words. In all of these, knowing when to reach for indexOf versus equals versus substring is the difference between earning points and writing a tangled loop. Two skills matter most. You have to trace what index comes back (including the first-occurrence rule), and you have to handle the -1 case before using the result as an index, because substring(-1) throws a StringIndexOutOfBoundsException.
They're inverses. charAt(int index) takes an index and returns the single character at that position, while indexOf(String str) takes a substring and returns the index where it starts. If the question gives you a position and asks for a character, that's charAt. If it gives you text to search for and asks where it is, that's indexOf. Also note the return types differ. charAt returns a char, and indexOf returns an int.
indexOf(String str) returns the index of the first occurrence of str in the string, or -1 if str doesn't appear anywhere.
A return of -1 is a normal value, not an exception, so exam code checks it with an if statement before doing anything with the index.
indexOf only finds the first match, so "banana".indexOf("an") returns 1 even though "an" also appears at index 3.
Strings are immutable, so indexOf never changes the string; it just reports a position.
The most common exam pattern pairs indexOf with substring, using the returned index to slice, delete, or rearrange parts of a string.
Passing an indexOf result of -1 into substring causes a StringIndexOutOfBoundsException, which is a classic FRQ point-loser.
indexOf(String str) searches the calling string from left to right and returns the index where str first occurs, or -1 if it's not found. It's on the Java Quick Reference, so its signature is given to you on the exam.
No. indexOf never throws an exception for a missing substring; it returns -1. The error only happens later if you pass that -1 into a method like substring without checking it first.
indexOf takes a substring and returns an int index telling you where it starts. charAt takes an int index and returns the char at that position. They go in opposite directions, and AP MCQs frequently test whether you know which is which.
No, it only returns the first occurrence. To find later occurrences, you combine indexOf with a loop or repeated substring calls, a pattern that shows up in string-processing FRQs like 2017's Phrase question.
Yes. It's required under EK 1.15.B.2 in Topic 1.15 (String Methods) and appears on the Java Quick Reference you get during the exam. Released FRQs from 2017, 2021, and 2024 all involved the kind of string searching and comparison indexOf handles.
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
Review units, study guides, and course resources.
Check this vocabulary in multiple-choice context.
Apply key concepts in written AP responses.
Estimate the exam score you are working toward.
Review the highest-yield facts before practice.
Put the full course together before test day.