Java String compareTo() method example

Java String compareTo() method compares two strings lexicographically. We can consider it dictionary based comparison.

1. String comparison

If a string 'str1' comes before another string 'str2' in dictionary, then str2 is said to be greater than 'str1' in string comparison.

string1 > string2‘string1’ comes AFTER ‘string2’ in dictionary.
string1 < string2 - 'string1' comes BEFORE 'string2' in dictionary.
string1 = string2 - 'string1' and 'string2' are equal.

2. String compareTo() method

In compareTo() method, two strings are compared lexicographically (dictionary order). The first string is the String object itself on which method is called. Second string is argument to method.

This method does the string comparison based on the Unicode value of each character in the strings.

2.1. Method return type

The result of this method is in integer value where -

  1. positive integer - means string object lexicographically follows the argument string.
  2. negative integer - means string object lexicographically precedes the argument string.
  3. zero - means both strings are equal.

2.2. Method syntax

Java compareTo() method implementation.

public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}

3. Java String compareTo() example

Learn how to call compareTo() method on Java strings.

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( "apple".compareTo("banana") );  //-1 - apple comes before banana
        System.out.println( "apple".compareTo("cherry") );  //-2 - apple comes before cherry
        System.out.println( "cherry".compareTo("banana") ); //1  - cherry comes after banana
        System.out.println( "cherry".compareTo("cherry") ); //0  - Both strings are equal
    }
}

4. Java String compareToIgnoreCase() example

Java program to compare two strings in case-insensitive manner. Notice that compareTo() and compareToIgnoreCase() methods behave in same way, except later is case-insensitive.

In given example, notice the string comparison in first two statements, how changing the case of string may change the result and ordering.

Note again the comparison is done character by character for both strings - after converting each character to unicode value.

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( "apple".compareTo("BANANA") );                     //31
        System.out.println( "apple".compareToIgnoreCase("banana") );            //-1
        
        System.out.println( "cherry".compareTo("cherry") );                     //0
        System.out.println( "cherry".compareToIgnoreCase("CHERRY") );           //0
    }
}

Happy Learning !!

Reference: String Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.