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.
Character | ASCII Value | Character | ASCII Value |
---|---|---|---|
a | 97 | A | 65 |
b | 98 | B | 66 |
c | 99 | C | 67 |
d | 100 | D | 68 |
e | 101 | E | 69 |
f | 102 | F | 70 |
g | 103 | G | 71 |
h | 104 | H | 72 |
i | 105 | I | 73 |
j | 106 | J | 74 |
k | 107 | K | 75 |
l | 108 | L | 76 |
m | 109 | M | 77 |
n | 110 | N | 78 |
o | 111 | O | 79 |
p | 112 | P | 80 |
q | 113 | Q | 81 |
r | 114 | R | 82 |
s | 115 | S | 83 |
t | 116 | T | 84 |
u | 117 | U | 85 |
v | 118 | V | 86 |
w | 119 | W | 87 |
x | 120 | X | 88 |
y | 121 | Y | 89 |
z | 122 | Z | 90 |
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.