w3resource

Java String: compareTo() Method

compareTo() method - Compares two strings lexicographically

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

  • The result is a negative integer if this String object lexicographically precedes the argument string.
  • The result is a positive integer if this String object lexicographically follows the argument string.
  • The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

Note: If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length()-anotherString.length()

Java Platform: Java SE 8 and above

Syntax compareTo() method

compareTo(String anotherString)

Parameters compareTo() method

Name Description Type
anotherString the String to be compared. String

Return Value compareTo() method

  • The value 0 if the argument string is equal to this string.
  • A value less than 0 if this string is lexicographically less than the string argument.
  • A value greater than 0 if this string is lexicographically greater than the string argument.

Return Value Type: int

Example: Java String compareTo() Method

The following example shows the usage of java String() method.

public class Example {
public static void main(String[] args)
    {
        String str1 = "This is Exercise 1";
        String str2 = "This is Exercise 2";
System.out.println();
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2); 

        // Compare the two strings.
int result = str1.compareTo(str2);

        // Display the results of the comparison.
if (result < 0)
        {
System.out.println("\"" + str1 + "\"" +
" is less than " +
                "\"" + str2 + "\"");
System.out.println();
        }
else if (result == 0)
        {
System.out.println("\"" + str1 + "\"" +
" is equal to " +
                "\"" + str2 + "\"");
System.out.println();
        }
else // if (result > 0)
        {
System.out.println("\"" + str1 + "\"" +
" is greater than " +
                "\"" + str2 + "\"");
System.out.println();        
        }
    }
}

Output:

String 1: This is Exercise 1                           
String 2: This is Exercise 2                           
"This is Exercise 1" is less than "This is Exercise 2"

Java Code Editor:

Previous:codePointCount Method
Next:compareToIgnoreCase Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/string/string_compareto.php