A lexicographic comparison is something similar to the ordering that one might find in a dictionary.
Java String class provides the .compareTo() method in order to lexicographically compare Strings. It is used like this “kiwi”.compareTo(“pineapple”) .
The return of this method is an int which can be interpreted as follows:
then the string is calling the method that lexicographically comes first
then the two strings are lexicographically equivalent
then the parameter passed to the compareTo method is lexicographically first.
The method provides the first non-zero difference in ASCII values.
There is also a variant .compareToIgnoreCase() which will return 0 for “b”.compareToIgnoreCase(“B”);
Want to learn Java? Check out the Java certification from Intellipaat.