Home Java Character Methods compare() Java Character compare() Method 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.
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 Call What 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.lang.Character (since Java 7)
public static int compare(char x, char y) Parameters Parameter Description x (char)The first char. y (char)The second char.
Return Value Return Value Description negative int x < y.0x == y.positive int x > y.
Compare with Similar Methods Other ways to compare:
Method How 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 1. Which char is smaller 2. Case-insensitive sort 3. Compare text by hand Example 1 — Compare Pairs of Chars and Print the Sign Compare four pairs. Integer.signum turns each result into -1, 0, or 1.
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));
}
}
} a vs b: -1
b vs a: 1
z vs z: 0
Z vs a: -1 How It Works 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.Reduce to the sign. Integer.signum keeps the output independent of how large the difference is.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.
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 Compare lowercase forms. The comparator turns B into b and C into c before calling Character.compare.Original chars are kept. Only the comparison uses lowercase; the array still contains 'B' and 'C'.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.
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 + ")");
}
}
} apple vs apply: -1 (agrees: true)
pear vs peach: 1 (agrees: true)
go vs going: -1 (agrees: true)
same vs same: 0 (agrees: true) How It Works First difference decides. apple and apply differ at index 4: 'e' is smaller than 'y', so the result is negative.Later chars do not matter. pear vs peach is decided at index 3, where 'r' is greater than 'c'.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 What does Character.compare return? A negative number if x < y, 0 if they are equal, and a positive number if x > y.
Which Java version added Character.compare? Java 7. On older versions, use Character.valueOf(x).compareTo(Character.valueOf(y)).
Does it return x - y? OpenJDK does, but only the sign is guaranteed by the documentation. Test < 0, == 0, or > 0.
Is Character.compare case-insensitive? No. It compares raw codes. Convert both chars with Character.toLowerCase first for a simple case-insensitive check.
What is the difference from compareTo? compare is static and takes two primitive chars; compareTo is called on a Character object and takes another Character.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion