Variants of CompareTo() Method

There are three variants of the compareTo() method which are as follows:

  1. using int compareTo(Object obj)
  2. using int compareTo(String AnotherString)
  3. using int compareToIgnoreCase(String str)  

1. int compareTo(Object obj)

This method compares this String to another Object.

Syntax: 

int compareTo(Object obj)

Parameters: 

obj:  the Object to be compared.

Return Value: The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.

Example:

Below is the implementation of int compareTo(Object obj):

Java




// Java code to demonstrate the
// working of compareTo()
public class Cmp1 {
    public static void main(String args[])
    {
 
        // Initializing Strings
        String str1 = "w3wiki";
        String str2 = new String("w3wiki");
        String str3 = new String("astha");
 
        // Checking if w3wiki string
        // equates to w3wiki object
        System.out.print(
            "Difference of w3wiki(obj) and w3wiki(str) : ");
        System.out.println(str1.compareTo(str2));
 
        // Checking if w3wiki string
        // equates to astha object
        System.out.print(
            "Difference of astha(obj) and w3wiki(str) : ");
        System.out.println(str1.compareTo(str3));
    }
}


Output

Difference of w3wiki(obj) and w3wiki(str) : 0
Difference of astha(obj) and w3wiki(str) : 6



2. int compareTo(String anotherString) 

This method compares two strings lexicographically. 

Syntax: 

int compareTo(String anotherString)

Parameters: 

anotherString:  the String to be compared.

Return Value: The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.

Example:

Below is the implementation of int compareTo(String anotherString):

Java




// Java code to demonstrate the
// working of compareTo()
 
public class Cmp2 {
    public static void main(String args[])
    {
 
        // Initializing Strings
        String str1 = "w3wiki";
        String str2 = "w3wiki";
        String str3 = "astha";
 
        // Checking if w3wiki string
        // equates to w3wiki string
        System.out.print(
            "Difference of w3wiki(str) and w3wiki(str) : ");
        System.out.println(str1.compareTo(str2));
 
        // Checking if w3wiki string
        // equates to astha string
        System.out.print(
            "Difference of astha(str) and w3wiki(str) : ");
        System.out.println(str1.compareTo(str3));
    }
}


Output

Difference of w3wiki(str) and w3wiki(str) : 0
Difference of astha(str) and w3wiki(str) : 6



3. int compareToIgnoreCase(String str)  

This method compares two strings lexicographically, ignoring case differences. 

Syntax:

int compareToIgnoreCase(String str)

Parameters: 

str: the String to be compared.

Return Value: This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Example:

Below is the implementation of int compareToIgnoreCase(String str):

Java




// Java code to demonstrate the
// working of compareToIgnoreCase()
 
public class Cmp3 {
    public static void main(String args[])
    {
 
        // Initializing Strings
        String str1 = "geeks";
        String str2 = "gEEkS";
 
        // Checking if w3wiki string
        // equates to w3wiki string
        // case sensitive
        System.out.print(
            "Difference of geeks and gEEkS (case sensitive) : ");
        System.out.println(str1.compareTo(str2));
 
        // Checking if w3wiki string
        // equates to astha string
        // case insensitive
        // using compareToIgnoreCase()
        System.out.print(
            "Difference of geeks and gEEkS (case insensitive) : ");
        System.out.println(str1.compareToIgnoreCase(str2));
    }
}


Output

Difference of geeks and gEEkS (case sensitive) : 32
Difference of geeks and gEEkS (case insensitive) : 0



Java String compareTo() Method with Examples

Strings in Java are objects that are supported internally by an array only which means contiguous allocation of memory for characters .  Please note that strings are immutable in Java which means once we create a String object and assign some values to it, we cannot change the content. However we can create another String object with the modifications that we want.

The String class of Java comprises a lot of methods to execute various operations on strings and we will be focusing on the Java String compareTo() method in this article.

Similar Reads

Java String.compareTo() Method

The Java compareTo() method compares the given string with the current string lexicographically. It returns a positive number, a negative number, or 0. It compares strings based on the Unicode value of each character in the strings....

Syntax

...

Variants of CompareTo() Method

int comparison = str1.compareTo(str2);...

Exceptions in Java String compareTo() Method

There are three variants of the compareTo() method which are as follows:...

Conclusion

...

Java String CompareTo() Method- FAQs

...