In java if
s1 = "d";
s2 = "D"
System.out.println(s1.compareTo(s2));
It will give output 32 always no matter what alphabet you use if they are same i.e. one is capital and one is smaller.
Why is this so???
In java if
s1 = "d";
s2 = "D"
System.out.println(s1.compareTo(s2));
It will give output 32 always no matter what alphabet you use if they are same i.e. one is capital and one is smaller.
Why is this so???
Here's the javadoc of compareTo
method and this is what it says:
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.
So, it compares based on the unicode value of characters. As D
and d
are different characters, they have different unicode value and hence, comparison does not return 0. Also, as per this Unicode chart, capital letters have lower unicode value than small letters and hence, you get positive value.
If you want case insensitive comparison then you should probably use compareToIgnoreCase.
String s1 = "d"; // Ascii value for 'd' 100
String s2 = "D"; //Ascii value for 'D' 68
System.out.println(s1.compareTo(s2));
Hence 32 (100-68) is the output
What compareTo returns, depends on how it's implemented by a particular class that implements Comparable interface, compareTo method returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
See javadoc, for example here: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)
In the Comparable<T>
's compareTo
should be implemented like this:
this
is larger than the parameter, return a positive numberthis
is equal to the parameter, return 0this
is less than the parameter, return a negative number.From this, we can see that an easy to implement a compareTo
method that compares integers is simply to subtract the parameter from this
. Well, characters can be represented as integers, can't they?
The implementation of the compareTo
method that you're using is probably to convert both this
and the parameter to its Unicode value and subtract the latter from the former.
Let me explain why you get 32 every time.
The Unicode value for "a" is 97 and that for "A" is 65. "b" is 98 and "B" is 66... and so on.
Now we can see that the difference in Unicode values between a lowercase letter and its uppercase counterpart is always 32. And since compareTo
is subtracting one value from the other, you always get 32.