---
title: "compareTo Method — AP Comp Sci A Definition & Exam Guide"
description: "compareTo is the Java String method that compares two strings lexicographically and returns a negative, zero, or positive int. A Topic 1.15 staple on AP CSA MCQs."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 1"
---

# compareTo Method — AP Comp Sci A Definition & Exam Guide

## Definition

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.

## What It Is

The `compareTo` [method](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") 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](/ap-comp-sci-a/key-terms/return-value "fv-autolink") 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](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink"). 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](https://library.fiveable.me/ap-comp-sci-a/unit-1/string-methods/study-guide), strings are immutable, so calling `compareTo` never changes either string. It just hands you a number.

## Why It Matters

compareTo lives in **Topic 1.15 (String Methods)** within **[Unit 1](/ap-comp-sci-a/unit-1 "fv-autolink"): Using Objects and Methods**, supporting learning objective **[AP Comp Sci A](/ap-comp-sci-a "fv-autolink") 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.

## Connections

### [equals method (Unit 1)](/ap-comp-sci-a/key-terms/equals-method)

equals and compareTo are siblings, but they answer different questions. equals returns a [boolean](/ap-comp-sci-a/unit-1/variables-and-primitive-data-types/study-guide/rezA6f3hJz84TKaY5Jjl "fv-autolink") 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)](/ap-comp-sci-a/key-terms/charat-method)

compareTo is essentially charAt running in a [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") 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)](/ap-comp-sci-a/key-terms/indexof)

Both [indexOf](/ap-comp-sci-a/key-terms/indexof "fv-autolink") 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)](/ap-comp-sci-a/key-terms/string-literal)

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.

## On the AP Exam

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.

## compareTo method vs equals method

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`.

## Key Takeaways

- 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.

## FAQs

### What does the compareTo method do in Java?

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.

### Does compareTo return exactly -1, 0, or 1?

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.

### What's the difference between compareTo and equals?

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.

### Is compareTo on the AP Computer Science A exam?

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.

### Why does compareTo say "Zebra" comes before "apple"?

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.

## Related Study Guides

- [1.15 String Methods](/ap-comp-sci-a/unit-1/string-methods/study-guide/SltCtk8JxBIgHcMfG6G4)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method#resource","name":"compareTo Method — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:20.003Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method#term","name":"compareTo method","description":"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.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/compareto-method","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What does the compareTo method do in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Does compareTo return exactly -1, 0, or 1?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What's the difference between compareTo and equals?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Is compareTo on the AP Computer Science A exam?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Why does compareTo say \"Zebra\" comes before \"apple\"?","acceptedAnswer":{"@type":"Answer","text":"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."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 1","item":"https://fiveable.me/ap-comp-sci-a/unit-1"},{"@type":"ListItem","position":4,"name":"compareTo method"}]}]}
```
