String
class implements Comparable
interface and we can see that the results are the lexicographic difference between two strings, which makes sense:
String s1 = "AB";
String s2 = "AF";
System.out.println(s1.compareTo(s2));
Will output -4
, whereas if we switch the arguments outputs 4
.
What I don't understand is the comparison with strings with different cases, i.e:
String s1 = "Ac";
String s2 = "AD";
System.out.println(s1.compareTo(s2));
This will output 31
it just gives me the difference between the two first different charaters in the string according to the ASCII table, it's telling me "Ac"
is larger than "AD"
by 31
.
Now, I know this is easily fixable by having a custom comparator, anyway I fail to see how this result given by the default comparator could be of any use, what is the purpose of it?
When I compare strings I often just want to know if they are equal and less often if one is larger than the other regardless if they are upper or lower case, wouldn't compare case insensitive by default be better?