In Java, a.compareTo(b) compares two Character objects by their numeric char values. It returns a negative number when a comes first, zero when they are equal, and a positive number when a comes later.
This method is why Character implements Comparable<Character>: sorting methods and sorted collections use it to put characters in order. The order is by code, so all uppercase ASCII letters come before all lowercase ones.
An instance method of java.lang.Character (Java 1.2+): call it on a Character object, as in boxed.compareTo(...), not on the class name. No import is needed, because java.lang is always available.
Syntax
Call it on one Character and pass the other.
Java
Character a = 'a';
a.compareTo('c'); // negative ('a' is 97, 'c' is 99)
a.compareTo('a'); // 0
a.compareTo('A'); // positive ('A' is 65)
Call
What it does
a.compareTo(b) < 0
Does a sort before b?
a.compareTo(b) == 0
Same char value.
(x, y) -> y.compareTo(x)
A comparator for reverse order.
One overload, compareTo(Character anotherCharacter), since Java 1.2. Passing a char works too, because it is autoboxed.
Library declarations
Java
// java.lang.Character (since Java 1.2)
public int compareTo(Character anotherCharacter)
Example 1 — Compare Letters and See Which Comes First
Compare lowercase and uppercase letters. Integer.signum reduces each result to -1, 0, or 1.
Java
public class CharacterCompareToBasic {
public static void main(String[] args) {
Character a = 'a', c = 'c', upper = 'A';
System.out.println("a vs c: " + Integer.signum(a.compareTo(c)));
System.out.println("c vs a: " + Integer.signum(c.compareTo(a)));
System.out.println("a vs a: " + a.compareTo('a'));
System.out.println("A vs a: " + Integer.signum(upper.compareTo(a)));
}
}
Output
a vs c: -1
c vs a: 1
a vs a: 0
A vs a: -1
How It Works
Order by code.'a' is 97 and 'c' is 99, so a comes first and the result is negative.
Equal values give 0. The char argument 'a' is autoboxed to a Character with the same value.
Uppercase first.'A' is 65, smaller than every lowercase letter, so upper.compareTo(a) is negative.
Example 2 — Find the Smallest and Largest Character, and Handle null
Scan an array with compareTo, then see what happens when the argument is null.
Java
public class CharacterCompareToMinMax {
public static void main(String[] args) {
Character[] letters = {'m', 'C', 'x', 'a'};
Character min = letters[0], max = letters[0];
for (Character c : letters) {
if (c.compareTo(min) < 0) min = c;
if (c.compareTo(max) > 0) max = c;
}
System.out.println("min=" + min + " max=" + max);
Character missing = null;
try {
min.compareTo(missing);
} catch (NullPointerException e) {
System.out.println("compareTo(null): NullPointerException");
}
}
}
Output
min=C max=x
compareTo(null): NullPointerException
How It Works
Only the sign matters. The loop tests < 0 and > 0, which is all the method guarantees.
Code order, not alphabet order.'C' (67) beats 'a' (97) as the minimum, because uppercase codes are smaller.
null is not allowed.compareTo needs the char inside the argument, so a null argument throws NullPointerException.
Example 3 — Sort a Character Array Forward and Backward
Arrays.sort uses compareTo for natural order; a lambda that swaps the arguments reverses it.
Java
import java.util.Arrays;
public class CharacterCompareToSort {
static String join(Character[] arr) {
StringBuilder sb = new StringBuilder();
for (Character c : arr) sb.append(c).append(' ');
return sb.toString().trim();
}
public static void main(String[] args) {
Character[] arr = {'d', 'B', 'a', 'C'};
Arrays.sort(arr);
System.out.println("natural: " + join(arr));
Arrays.sort(arr, (x, y) -> y.compareTo(x));
System.out.println("reversed: " + join(arr));
}
}
Output
natural: B C a d
reversed: d a C B
How It Works
Comparable in action.Arrays.sort(arr) calls compareTo on the elements, giving B (66), C (67), a (97), d (100).
Swap to reverse. The lambda (x, y) -> y.compareTo(x) asks the opposite question, so the largest code comes first.
Objects, not primitives. The array is Character[]; a comparator cannot be used with a primitive char[].
Frequently Asked Questions
A negative number if this char is smaller, 0 if equal, and a positive number if larger, comparing char codes.
OpenJDK returns this value minus the other value, but the specification only guarantees the sign, so do not rely on the number.
compareTo compares codes, and A-Z (65-90) are smaller than a-z (97-122).
It throws NullPointerException.
compareTo is an instance method on Character objects; Character.compare(x, y) is static, takes two primitive chars, and was added in Java 7.