How does String compareTo() method work in Java

The compareTo() method is defined in the Comparable interface which returns an int value that can be a negative number, zero, or positive numbers.  This method is overridden in the String class, which compares two String lexicographically.

Let’s understand lexicographically meaning with an example. Suppose we have two characters a and c The character a comes before c this is called the lexicographical order.  Again the question may come, why we are not calling this alphabetical order? a and c are nothing but alphabet only right? when we say lexicographically order we mean to say something more than the alphabet like some special character too.

Let’s see how compareTo(Object 0) return int value. Suppose we two String s1 = "am"; and String s2 = "bm";.

  • if (s1 > s2) it returns a positive number.
  • if both the strings are equal, it returns 0.
  • if (s1< s2) it returns a negative number.

To understand how does String compareTo() method work, consider the below example.

public class ComareToExample1 {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "bcd";
		System.out.println(s1.compareTo(s2));
	}
}

Output :- -1

The output of the above program is -1. It will compare the first character of s1 and s2, which is a and b. It will check the ASCII value of a and b which is 97 and 98, since we are doing s1.comapreTo(s2) it will calculate 97-98 which is -1. So, what about the rest of the characters(i.e bc and cd), will it check for those characters, no it will not check because we have a different character in beginning only(s1 has a and s2 has b). Before going forward let’s have a look in the ASCII table, which will help us to discuss further.

CharacterASCII ValueCharacterASCII Value
a97A65
b98B66
c99C67
d100D68
e101E69
f102F70
g103G71
h104H72
i105I73
j106J74
k107K75
l108L76
m109M77
n110N78
o111O79
p112P80
q113Q81
r114R82
s115S83
t116T84
u117U85
v118V86
w119W87
x120X88
y121Y89
z122Z90

Let’s see another example that will help to understand how String compareTo() method work.

public class CompareToExample2 {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "ade";
		System.out.println(s1.compareTo(s2));
	}
}

Output:- -2

The output of the above program is -2. Let’s see how.

The String s1 is referring to “abc” and s2 is referring to “ade”, in this first character is the same so it will compare the next character i.e b and d. ASCII value of b and d is 98 and 100 respectively and we are comparing s1.compareTo(s2), it will calculate 98-100 and the result is -2. Again what will happen if we do s2.compareTo(s1)? The result would be  2.

Another example

public class CompareToExample3 {
	public static void main(String[] args) {
		String s1 = "Abc";
		String s2 = "ade";
		System.out.println(s1.compareTo(s2));
	}
}

Output:- -32

s1 is referring to “Abc” and s2 is referring “abc”, the ASCII value of A is 65, and the ASCII value of a is 97. The calculation would be something like 65-97 which is -32.

Observe the below example

public class CompareToExample2 {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		System.out.println(s1.compareTo(s2));
	}
}

Output:- 0

It will compare all characters, will check for ASCII values, and finally it will return 0.

That’s all about how does String compareTo() method work in Java.

See docs for more details.