CodeGym/Java Blog/Strings in Java/Java String compareTo() Method
Author
Milan Vucic
Programming Tutor at Codementor.io

Java String compareTo() Method

Published in the Strings in Java group
members

What is Java String compareTo() Method?

The java string class compareTo() method returns the 0 value if both strings are lexicographically equal. If the compared string is greater lexicographically then the positive value is returned otherwise the negative value is returned. So the Java string compareTo() method is used to compare two strings. The unicode value of each character in the string is always used by this method to compare them. While comparing the strings, if any of them is empty, it always returns the length of the string. If any of the strings is empty, then two scenarios can play out. If the first string is empty then it returns a negative value, otherwise it returns a positive value. The Java string.compareTo() method by default is case sensitive but we can use Java String class compareToIgnoreCase() method to ignore case sensitivity during comparison. This method also returns a negative, 0, or a positve integer as described above.

Syntax

public int compareTo(string str)
public int compareTo(object obj)

Parameters

The Java string compareTo() method receives a string or an object as a parameter as you can see above in the syntax.

Returns

  1. It returns 0 if both are lexicographically equal.
  2. It returns positive integer if compared string or object is greater lexicographically.
  3. It returns negative integer if one of the compared is smaller lexicographically.

Exceptions

The compareTo() method returns 2 exceptions.
  • ClassCastException, if the object can not be compared then it returns this exception.
  • NullPointerException, if the string is null then NullPointerException is thrown.

Java String compareTo() Method Examples

class Main {
  public static void main(String[] args) {
    // declaring strings to be used in this example for Java string     compareTo() method
    String str = "Java compareTo() method example";
    String str1 = "Java compareTo() method example";
    String str2 = "this is Java compareTo() method example";
    String str3 = "Java CompareTo() Method Example";
    String str4 = "a Java compareTo() method example";
    String str5 = new String("Java compareTo() method example");

    // comparing the str and str1 strings
    System.out.println(str.compareTo(str1));

    // comparing the str and str2 strings
    System.out.println(str.compareTo(str2));

    // comparing the str and str3 strings
    System.out.println(str.compareTo(str3));

    // comparing the str and str4 strings
    System.out.println(str.compareTo(str4));

    // comparing the str string and str5 string object
    System.out.println(str.compareTo(str5));
  }
}

Output

0 -42 74 -23 0
As we know the str and str1 strings are equal lexicographically so it returned 0. While comparing str and str2 it returned negative 42 value because the compared string str is smaller lexicographically so it’s a negative and on the basis of unicode value t char and J char has a difference of 42, so a -42 value is returned. For the rest you can see the results to better understand this method.

Example of an Empty String

public class Main{
    public static void main(String args[]){
        String str="compareTo()";

        // declaring an empty string
        String str1="";
        String str2="method";

        System.out.println(str.compareTo(str1));
        System.out.println(str1.compareTo(str2));
    }
}

Output

11 -6
Java String compareTo() Method - 1

Example of IgnoreCase

public class Main{
    public static void main(String args[]){
        String str="compareTo()";

        // declaring the same string with uppercase letters
        String str1="COMPARETO()";

        System.out.println(str.compareTo(str1));
        System.out.println(str.compareToIgnoreCase(str1));
    }
}

Output

32 0

Conclusion

We hope by now you understand what is the Java string compareTo() method, and how to implement it for string comparison, along with its different user cases, such as the empty strings comparison and ignoring case sensitivity. Feel free to practice and get back whenever you need more assistance. Happy learning!
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet