-4

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???

JRG
  • 4,037
  • 3
  • 23
  • 34
  • 4
    Please read the relevant [javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareTo-java.lang.String-) before asking something like this. See also [`compareToIgnoreCase`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-) – janos Jul 15 '17 at 08:38
  • Possible duplicate of [compareTo method java](https://stackoverflow.com/questions/10017381/compareto-method-java) – JRG Jul 15 '17 at 08:55
  • I don't think you tested all 109591 letters, even those that can be put into symmetric pairs of uppercase and lowercase. `"θ".compareTo("ϴ") == -60` theta: [uppercase](http://www.fileformat.info/info/unicode/char/03f4/index.htm) and [lowercase](http://www.fileformat.info/info/unicode/char/03b8/index.htm). Curiosities aside, most of the answers point out that the defined return values for the method are 0 and non-zero. – Tom Blodget Jul 15 '17 at 18:18
  • As for the curiosity, repeated comparison is sometimes implemented as a loop with a subtraction instruction, and then a branch-if-not-zero instruction to break out of the loop early, and ultimately, in all cases, returning the result of the last subtraction. The details would depend on the CPU or VM, in this case, the Java Virtual Machine. – Tom Blodget Jul 15 '17 at 18:35

4 Answers4

1

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.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1
    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

Isha Agarwal
  • 420
  • 4
  • 12
1

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)

ugo
  • 284
  • 1
  • 2
  • 10
1

In the Comparable<T>'s compareTo should be implemented like this:

  • If this is larger than the parameter, return a positive number
  • If this is equal to the parameter, return 0
  • If this 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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313