String.compareTo() in Java
Fast recall
Quick revision
Core answer
String.compareTo() returns zero for equal strings, a negative value when the caller is lexicographically smaller, and a positive value when it is larger. The magnitude is not restricted to -1 or 1. Ordering is based on UTF-16 code units, is case-sensitive, and is not dictionary or locale-aware; use Collator for human-language sorting.Whether you understand the three-way return value semantics and know the difference between equals() (boolean) and compareTo() (ordering).
Thinking compareTo() just returns -1, 0, or 1. It returns the actual character difference — 'b'.compareTo('a') returns 1, but 'hello'.compareTo('abc') returns 7 (difference in Unicode values of 'h' minus 'a').
Mention compareToIgnoreCase() and that compareTo() is what you implement when a class implements Comparable<T> — it enables sorting via Collections.sort() and TreeMap keys.
Interview-ready guide
Interview answer
The key shift from equals() is that compareTo() does not just tell you whether two Strings are the same — it tells you their order, which is what you need for sorting. It hands back an int: zero means equal, a negative number means the calling String comes first alphabetically, and a positive number means it is lexicographically larger.
The detail people get wrong is assuming that number is always -1, 0, or 1. It is not — it is the difference in Unicode values at the first character where the two Strings diverge. So "hello".compareTo("abc") returns 7, not 1. The practical rule is to branch on the sign only — < 0, == 0, > 0 — and never compare against a literal 1.
The other thing I would flag is case. Uppercase letters have lower Unicode values than lowercase, so "Apple" sorts before "apple", and a naive sort drops every capitalised word to the top. When case should not matter, compareToIgnoreCase() handles it.
Where this really earns its keep is Comparable — when you implement compareTo() on your own class, you usually delegate to a String field's compareTo(), and that is what lets Collections.sort(), Arrays.sort(), and TreeMap order your objects for free. So I think of equals() for yes/no and compareTo() for ranking — and I always check the sign, not the number.
String.compareTo() in Java
Direct Answer
String.compareTo() returns zero for equal strings, a negative value when the caller is lexicographically smaller, and a positive value when it is larger. The magnitude is not restricted to -1 or 1. Ordering is based on UTF-16 code units, is case-sensitive, and is not dictionary or locale-aware; use Collator for human-language sorting.
Code Example
import java.util.Arrays;
public class CompareToDemo {
public static void main(String[] args) {
System.out.println("apple".compareTo("apple")); // 0 — equal
System.out.println("apple".compareTo("banana")); // negative — apple comes before banana
System.out.println("banana".compareTo("apple")); // positive — banana is lexicographically larger apple
// Sorting strings alphabetically — compareTo() is used internally
String[] fruits = {"Mango", "Apple", "Banana", "Cherry"};
Arrays.sort(fruits);
System.out.println(Arrays.toString(fruits)); // [Apple, Banana, Cherry, Mango]
// Case-insensitive ordering
System.out.println("Apple".compareTo("apple")); // negative — 'A'(65) < 'a'(97)
System.out.println("Apple".compareToIgnoreCase("apple")); // 0 — case ignored
// Manual comparison — check sign only
String s1 = "orange";
String s2 = "grape";
if (s1.compareTo(s2) > 0) {
System.out.println(s2 + " comes before " + s1); // grape comes before orange
}
}
}
Common Mistakes & Pitfalls
- Assuming it returns only
-1,0, or1— it returns the actual Unicode gap, so"hello".compareTo("abc")is7; branch on the sign, never on== 1or== -1. - Using
compareTo()when you only need equality — if the question is are these the same?,equals()is clearer and faster; reservecompareTo()for ordering. - Forgetting case ranks below lowercase —
'A'(65) sorts before'a'(97), so"Zebra"lands before"apple"; switch tocompareToIgnoreCase()for human-friendly order. - Calling it on
null—compareTo(null)throwsNullPointerException; guard first or order nulls explicitly with aComparator.
Check your understanding
Answer these without looking back at the article.
- What is the Comparable interface in Java and how does it relate to compareTo()?
- What is the difference between Comparable and Comparator?
- How does TreeMap use compareTo() to maintain sorted order?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.