In AP Computer Science A, compareTo is a String method that compares two strings lexicographically (dictionary-style, by character codes) and returns a negative int if the calling string comes first, 0 if the strings are identical, and a positive int if the calling string comes after the argument.
The compareTo method answers the question "which of these two strings comes first?" When you write str1.compareTo(str2), Java walks through both strings character by character and compares them lexicographically, which is basically dictionary order based on character values. The return value is an int, and the sign is what matters. A negative number means str1 comes before str2, zero means they're identical, and a positive number means str1 comes after str2.
A helpful way to read it is as subtraction. Think of str1.compareTo(str2) as roughly "str1 minus str2." If the result is negative, str1 is "smaller" (earlier alphabetically). One catch worth knowing for the exam: lexicographic order uses character codes, so all uppercase letters come before all lowercase letters. That means "Zebra".compareTo("apple") is negative, even though Z comes after A in the alphabet. Also, per EK 1.15.A.3, strings are immutable, so calling compareTo never changes either string. It just hands you a number.
compareTo lives in Topic 1.15 (String Methods) within 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. It's one of the String methods on the Java Quick Reference you get during the exam, so the College Board expects you to know its signature and behavior cold. Beyond Unit 1, compareTo is how you make ordering decisions with strings, which feeds directly into boolean expressions and if-statements. Any time a problem says "sort these names" or "find the alphabetically first word," compareTo is the tool doing the work.
Keep studying AP® Computer Science A Unit 1
equals method (Unit 1)
equals and compareTo are siblings, but they answer different questions. equals returns a boolean answering "are these the same?" while compareTo returns an int answering "which one comes first?" If a.compareTo(b) returns 0, then a.equals(b) is true, and vice versa.
charAt method (Unit 1)
compareTo is essentially charAt running in a loop behind the scenes. Java compares the characters at index 0, then index 1, and so on until it finds a mismatch. Understanding character-by-character comparison explains why "apple".compareTo("application") is negative: they match for five characters, then "apple" runs out first.
indexOf (Unit 1)
Both indexOf and compareTo return ints, and the exam loves testing whether you know what each int means. indexOf returns a position (or -1 if not found), while compareTo returns a sign indicating order. Mixing up these return values is a classic MCQ trap.
string literal (Unit 1)
You can call compareTo directly on a string literal or on a string built through concatenation, since both create String objects (EK 1.15.A.1). Exam questions often chain operations, like concatenating with + or substring first, then comparing the result with compareTo.
compareTo shows up in multiple-choice questions that give you a code segment and ask what an expression evaluates to. A typical stem builds two strings, calls compareTo in both directions, and asks about the sign of each result. For example, with "apple".compareTo("application") you need to recognize that "apple" is a prefix of "application," so the shorter string comes first and the result is negative. Reversing the call flips the sign. Harder versions combine compareTo with concatenation or substring, like building "Java" + "ramm".substring(...) and then comparing the result to "JavaScript", so you have to trace the string operations correctly before you even get to the comparison. Two things to lock in for these problems. First, only the sign is guaranteed, so never assume a specific value like -1 or 1. Second, remember that compareTo is on the Java Quick Reference, so you don't need to memorize the signature, just what the return value means.
Both compare strings, but they return different types and answer different questions. equals returns a boolean (true or false) telling you whether two strings have identical content. compareTo returns an int whose sign tells you the ordering. A common mistake is writing if (str1.compareTo(str2)) as if it returned a boolean. It doesn't compile, because compareTo gives you an int that you must compare to 0, like str1.compareTo(str2) < 0.
compareTo compares two strings lexicographically and returns a negative int if the calling string comes first, 0 if the strings are equal, and a positive int if the calling string comes after the argument.
Only the sign of the return value is guaranteed, so never write code or pick an answer that depends on compareTo returning exactly -1 or 1.
Lexicographic order is based on character codes, which means every uppercase letter comes before every lowercase letter, so "Zebra" comes before "apple".
If one string is a prefix of another, like "apple" and "application", the shorter string comes first and compareTo returns a negative value.
compareTo never modifies either string because String objects are immutable (EK 1.15.A.3).
compareTo is listed on the Java Quick Reference, so on exam day you can look up its signature, but you still need to know how to interpret the result.
It compares two strings character by character in lexicographic (dictionary) order and returns an int. str1.compareTo(str2) is negative if str1 comes first, 0 if they're identical, and positive if str1 comes after str2.
No, and this is a common misconception. Java only guarantees the sign of the result, not the magnitude. "apple".compareTo("application") might return -6, not -1, so always check the result with < 0, == 0, or > 0.
equals returns a boolean telling you if two strings have the same content, while compareTo returns an int telling you their relative order. Use equals when you only care about sameness and compareTo when you care about alphabetical ordering, like sorting.
Yes. It's part of Topic 1.15 (String Methods) under learning objective AP Comp Sci A 1.15.B, and it appears on the Java Quick Reference you receive during the exam. MCQs frequently ask you to determine the sign of a compareTo result.
Because lexicographic order uses character codes, and all uppercase letters have smaller values than all lowercase letters. So 'Z' is "less than" 'a', making "Zebra".compareTo("apple") negative even though that looks backwards alphabetically.
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.