String compareTo() method
compareTo()
method in java string is used to compare two strings.
With this comparison, you can determine which string out of the two comes first alphabetically.
As per java docs, compareTo()
method
Compares two strings lexicographically.
Here, lexicographically means in the natural alphabetic order. Example, “cat” comes first before “dog”.
compareTo()
method accepts a string as argument. It compares the string on which it is invoked with the argument string and returns an integer value which is one among -1, o and 1.
Meaning of return value of compareTo()
is:
0 : both the strings are same or equal.
-1 : string on which compareTo()
is called is lesser than the argument string. Lesser means it comes before alphabetically.
1 : string on which compareTo()
is called is greater than the argument string. Greater means it comes after alphabetically.
Method signature of compareTo()
method is
public int compareTo(String anotherString)
String compareTo() example
Below is an example program to show the usage of compareTo()
method.
public class CompareToDemo { public static void main(String[] args) throws IOException { String s1 = "hero"; String s2 = "help"; int result = s1.compareTo(s2); System.out.println("Result is: "+result); } }
This prints
Result is: 6
Here, “hero” is the first string and “help” is the second string.
compareTo()
compares each character of first string with corresponding character of second string till it finds a mismatch.
When a mismatch is found, it returns the difference between the ASCII values of those characters.
In this example, the result should be the difference between characters ‘r’ and ‘l’, which is 114 – 108 = 6.
Note that compareTo()
will compare two strings character by character and returns the difference between the values of the first non-matching characters.
String equality with compareTo()
compareTo()
can be used to test if two string are same or not. It will return 0 if the strings are equal.
So, by comparing the return value from compareTo()
with 0, we can check if the strings are same. Example,
public class CompareToDemo { public static void main(String[] args) throws IOException { String s1 = "hero"; String s2 = "hero"; if(s1.compareTo(s2) == 0) { System.out.println("Strings are equal"); } else { System.out.println("Strings are equal"); } } }
compareTo()
will treat strings with same characters but different case as different strings. That is, "Abc".compareTo("abc")
will not be 0.If you want to test if two strings are equal, irrespective of their case, then use
compareToIgnoreCase()
.It also returns an integer value, which is either -1, 0 or 1, with same meaning as
compareTo()
. Example,
public class CompareToDemo { public static void main(String[] args) throws IOException { String s1 = "hero"; String s2 = "Hero"; int ignoreCaseresult = s1.compareToIgnoreCase(s2); int result = s1.compareTo(s2); System.out.println("Result is: "+result); System.out.println("Ignore case result is: "+ignoreCaseresult); } }
which prints
Result is: 32
Ignore case result is: 0
which shows that compareToIgnoreCase()
considers the strings to be equal irrespective of their case.
Length difference
If there is a difference in length of two strings but their characters are equal. This means that all the characters in the smaller string are present in the bigger string at the same positions, such as “blog” and “blogger”.
In such case, compareTo()
will return the difference between the length of two strings. Example,
public class CompareToDemo { public static void main(String[] args) throws IOException { String s1 = "hero"; String s2 = "heroic"; int result = s1.compareTo(s2); System.out.println("Result is: "+result); } }
This prints
Result is: -2
First string is completely present in second string, compareTo()
will return the difference between the length of two strings, which is 2.
Since the first string is shorter than the second string, the result is negative.
compareTo() with null values
null
value cannot be used to compareTo()
.
If the string on which compareTo()
is called, you will straightaway get a NullPointerException, since we cannot invoke a method on null.
compareTo()
will not be called in this case.
If the argument string is null
, then compareTo()
will be called but it will throw a NullPointerException
. Example,
String s = "abc"; int result = s.compareTo(null);
Output will be
java.lang.NullPointerException: Cannot read field “value” because “anotherString” is null
This is a pretty common question that comes to the mind of every reader. Though both these methods compare two strings but there are differences between the two.
1.
compareTo()
tells which string comes first while equals()
tells whether two strings are same or not.
2. compareTo()
returns an integer while equals()
returns a boolean
value.
3. With compareTo()
, the order of strings is important, that is, "abc".compareTo("def")
and "def".compareTo("abc")
will return different results but with equals(), this kind of ordering does not matter.
4. If the argument string given to compareTo()
is null
, it will throw a java.lang.NullPointerException while equals()
will return false
for a null
argument.
So, "abc".compareTo(null)
is an exception while "abc".equals(null)
is false
.
Hope the article was useful.