compareTo () is used for comparing two strings lexicographically. Each character of both strings are converted into a Unicode value. However, if both the strings are equal, then this method returns 0 else it only result either negative or positive value.
Character b comes after a alphabetically. Hence output is 1
In this method, if the first string is always lexicographically higher than second string, it returns a positive number.
Java String compareTo () method is used to perform natural sorting on string. Natural sorting means the sort order which applies on the object, e.g., lexical order for String, numeric order for Sorting integers, etc.
On comparing these two string, if the outcome is 0, then the compareTo () method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.
If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.
If you compare string with blank or empty string, it returns length of the string. If second string is empty, result would be positive. If first string is empty, result would be negative.