In the event that the first string is lexicographically more greater than second string, it will give a positive number. On the off chance that the first string is not as much as second string lexicographically, it returns negative number and if the first string is lexicographically equal to the second string, it returns 0.
compareTo() method is used to perform natural sorting on a string. Natural sorting implies the sort arrange which applies on the object, e.g., lexical request for String, numeric request for Sorting whole numbers etc.
Note — StringParam represent string that is to be compared with current string. And this method return type is integer.(Read Also — Exception Handling in Java)
String CompareTo() java method Example-
Here we have five Strings and we are comparing them with each other using
Now you guys have fair idea on string compareto java, Kindly let me know understanding and your opinion/doubt in the comment box.