Java Character compare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Character.compare(x, y) compares two primitive char values by their codes. It returns a negative number if x < y, zero if they are equal, and a positive number if x > y.

It is the static, primitive counterpart of compareTo(): no Character objects are needed, and it is a handy building block for comparators. OpenJDK computes it as x - y, which cannot overflow because both chars are 0 to 65535.

A static method of java.lang.Character (Java 7+): call it on the class, as in Character.compare(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

Pass the two chars to compare.

Java
Character.compare('a', 'b');   // negative
Character.compare('b', 'a');   // positive
Character.compare('z', 'z');   // 0
Character.compare('Z', 'a');   // negative: 'Z' is 90, 'a' is 97
CallWhat it does
Character.compare(x, y) < 0Does x come first?
Character.compare(Character.toLowerCase(x), Character.toLowerCase(y))A simple case-insensitive comparison.
(x, y) -> Character.compare(x, y)A comparator for Character values.

One overload, compare(char x, char y), added in Java 7. On Java 6, use Character.valueOf(x).compareTo(Character.valueOf(y)), which gives the same result.

Library declarations
Java
// java.lang.Character (since Java 7)
public static int compare(char x, char y)

Parameters

ParameterDescription
x (char)The first char.
y (char)The second char.

Return Value

Return ValueDescription
negative intx < y.
0x == y.
positive intx > y.

Compare with Similar Methods

Other ways to compare:

MethodHow it differs
a.compareTo(b)The same comparison on Character objects.
x < yPlain operators when you only need one test.
String.compareTo()Compares whole strings, char by char.

Java Examples

Example 1 — Compare Pairs of Chars and Print the Sign

Compare four pairs. Integer.signum turns each result into -1, 0, or 1.

Java
public class CharacterCompareSigns {
    public static void main(String[] args) {
        char[][] pairs = { {'a', 'b'}, {'b', 'a'}, {'z', 'z'}, {'Z', 'a'} };
        for (char[] p : pairs) {
            int result = Character.compare(p[0], p[1]);
            System.out.println(p[0] + " vs " + p[1] + ": " + Integer.signum(result));
        }
    }
}

How It Works

  1. Pairs in an array. Each inner array holds the two chars; p[0] + " vs " starts with a char followed by a String, so it is concatenation, not addition.
  2. Reduce to the sign. Integer.signum keeps the output independent of how large the difference is.
  3. Code order. 'Z' (90) is smaller than 'a' (97), so uppercase letters sort first.

Example 2 — Sort Letters Ignoring Case

Lower-case both chars before comparing them, so 'B' and 'b' sort together.

Java
import java.util.Arrays;

public class CharacterCompareIgnoreCase {
    public static void main(String[] args) {
        Character[] letters = {'d', 'B', 'a', 'C'};
        Arrays.sort(letters, (x, y) -> Character.compare(Character.toLowerCase(x), Character.toLowerCase(y)));
        StringBuilder sb = new StringBuilder();
        for (Character c : letters) sb.append(c).append(' ');
        System.out.println(sb.toString().trim());
    }
}

How It Works

  1. Compare lowercase forms. The comparator turns B into b and C into c before calling Character.compare.
  2. Original chars are kept. Only the comparison uses lowercase; the array still contains 'B' and 'C'.
  3. Alphabetical result. Without the lowercase step, B and C would come before a and d, because uppercase codes are smaller.

Example 3 — Compare Two Strings Char by Char

Write a small version of string comparison with Character.compare and check it against String.compareTo.

Java
public class CharacterCompareText {
    static int compareText(String a, String b) {
        int n = Math.min(a.length(), b.length());
        for (int i = 0; i < n; i++) {
            int r = Character.compare(a.charAt(i), b.charAt(i));
            if (r != 0) return r;
        }
        return Integer.compare(a.length(), b.length());
    }

    public static void main(String[] args) {
        String[][] tests = { {"apple", "apply"}, {"pear", "peach"}, {"go", "going"}, {"same", "same"} };
        for (String[] t : tests) {
            int mine = Integer.signum(compareText(t[0], t[1]));
            boolean agrees = mine == Integer.signum(t[0].compareTo(t[1]));
            System.out.println(t[0] + " vs " + t[1] + ": " + mine + " (agrees: " + agrees + ")");
        }
    }
}

How It Works

  1. First difference decides. apple and apply differ at index 4: 'e' is smaller than 'y', so the result is negative.
  2. Later chars do not matter. pear vs peach is decided at index 3, where 'r' is greater than 'c'.
  3. Prefix rule. When one string is a prefix of the other, as go is of going, the shorter one comes first; this matches String.compareTo in every case.

Frequently Asked Questions

A negative number if x < y, 0 if they are equal, and a positive number if x > y.
Java 7. On older versions, use Character.valueOf(x).compareTo(Character.valueOf(y)).
OpenJDK does, but only the sign is guaranteed by the documentation. Test < 0, == 0, or > 0.
No. It compares raw codes. Convert both chars with Character.toLowerCase first for a simple case-insensitive check.
compare is static and takes two primitive chars; compareTo is called on a Character object and takes another Character.

Next: compareTo()

Compare two Character objects by their numeric char values.

compareTo() 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.

13 people found this page helpful