String compareTo() Java

String compareTo() Java | In Java, two strings can be compared by using the compareTo() method. This method compares two strings lexicographically that are in dictionary order.

Java String compareTo() method return type is an integer where it returns negative, positive, or 0 depending on the strings compared. If the first string is alphabetically greater than the second string then compareTo() returns a positive integer, if the first string is alphabetically lesser than the second string then the compareTo() method returns a negative integer, or else if the strings are equal then it returns 0.

Let us see some examples for String compareTo Java:-
Example-1:-
String1 = “Hello”
String2 = “Hello”
Result => 0

Example-2:-
String1 = “Abc”
String2 = “bca”
Result => 1

Example-3:-
String1 = “Opl”
String2 = “abc”
result => -8

The method signature of the Java compareTo Strings is:- public int compareTo(String str)

Parameters: str – the string which needs to be compared.
Return type: int
Returns:- positive, negative or zero if equal.
Exception: NulPointerException:- if the specified object is null; ClassCastException:- if the object cannot be compared with the other object.

Return Valuestr1.compareTo(str2)
0str1 is equal to str2
<0str1 is lexicographically less than str2
>0str1 is lexicographically greater than str2

How does string compareTo() work in Java? The compareTo() method compares each character of the given strings based on its Unicode value. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. Let us understand it through the program.

String compareTo() Java Example

public class Main {
   public static void main(String args[]) {
      String str1 = "Orange";
      String str2 = "Orange";
      String str3 = "mango";
      String str4 = "Apple";
      String str5 = "lemon";
      System.out.println(str1.compareTo(str2));
      System.out.println(str1.compareTo(str3));
      System.out.println(str1.compareTo(str4));
      System.out.println(str1.compareTo(str5));
   }
}

Output:-

0
-30
14
-29

The str1 and str2 contain the same strings with respect to data therefore compareTo method returns 0. Among “Orange” and “mango” the ‘O’ is lesser than ‘m’. The ASCII value of ‘O’ & ‘m’ is 79 & 109 respectively. When we subtract 79 – 109 it gives -30. Therefore “Orange”.compareTo(“mango”) returns -30.

Similarly when we compare “Orange” and “Apple” using the compareTo() method then it returns 14, because the ASCII value of ‘O’ & ‘A’ is 79 & 65 respectively. And 79 – 65 gives 14. Hence “Orange”.compareTo(“Apple”) returns 14.

public class Main {
   public static void main(String args[]) {
      String str1 = "apple";
      String str2 = "";
      String str3 = "apple";
      System.out.println(str1.compareTo(str2));
      System.out.println(str2.compareTo(str3));
      System.out.println(str1.compareTo(str3));
   }
}

Output:-

5
-5
0

compareToIgnoreCase() In String Java

Similar to the compareTo() method, the Java String class also contains the compareToIgnoreCase() method. It compares characters of string alphabetically without case-wise. It also returns an integer value.

public class Main {
   public static void main(String args[]) {
      String str1 = "apple";
      String str2 = "Apple";
      String str3 = "apple";
      System.out.println(str1.compareToIgnoreCase(str2));
      System.out.println(str2.compareToIgnoreCase(str3));
      System.out.println(str1.compareToIgnoreCase(str3));
   }
}

Output:-

0
0
0

Java String compareTo() vs equals()

Java String class also contains the equals() method to compare the strings. But there is a lot of difference between the compareTo() and equals() methods of the Java String class.

The equals() method return type is boolean and it returns either true or false. Whereas the compareTo() method returns integer values 0, <0, and >0. If both strings are content-wise same then equals() return true but compareTo() returns 0.

The equals() method is defined in the Java Object class and overridden in the String class whereas the compareTo() method is directly defined in the String class.

public class Main {
   public static void main(String args[]) {
      String str1 = "apple";
      String str2 = "Apple";
      String str3 = "apple";
      
      System.out.println(str1.compareTo(str2));
      System.out.println(str1.compareTo(str3));
      
      System.out.println(str1.equals(str2));
      System.out.println(str1.equals(str3));
   }
}

Output:-

32
0
false
true

Java String class contains equals(), equalsIgnoreCase(), compareTo(), and compareToIgnoreCase() methods to compare two string objects, we can use one of them based on our requirement.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *