Java Character compareTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

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)
CallWhat it does
a.compareTo(b) < 0Does a sort before b?
a.compareTo(b) == 0Same 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)

Parameters

ParameterDescription
anotherCharacter (Character)The object to compare with; must not be null.

Return Value

Return ValueDescription
negative intThis value is smaller.
0Both hold the same char.
positive intThis value is larger.

Compare with Similar Methods

Other ways to compare characters:

MethodHow it differs
Character.compare(x, y)Static version for two char values (Java 7+).
a.equals(b)Only answers "equal or not".
x < yPlain operators on primitive char values.

Java Examples

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)));
    }
}

How It Works

  1. Order by code. 'a' is 97 and 'c' is 99, so a comes first and the result is negative.
  2. Equal values give 0. The char argument 'a' is autoboxed to a Character with the same value.
  3. 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");
        }
    }
}

How It Works

  1. Only the sign matters. The loop tests < 0 and > 0, which is all the method guarantees.
  2. Code order, not alphabet order. 'C' (67) beats 'a' (97) as the minimum, because uppercase codes are smaller.
  3. 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));
    }
}

How It Works

  1. Comparable in action. Arrays.sort(arr) calls compareTo on the elements, giving B (66), C (67), a (97), d (100).
  2. Swap to reverse. The lambda (x, y) -> y.compareTo(x) asks the opposite question, so the largest code comes first.
  3. 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.

Next: digit()

Get the numeric value of a digit character in a given radix, or -1.

digit() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful