The general contract of Comparable.compareTo(o) is to return
- a positive integer if this is greater than the other object.
- a negative integer if this is lower than the other object.
- 0 if this is equals to the other object.
In your example "ab2".compareTo("ac3") == -1 and "ab2".compareTo("ab3") == -1 only means that "ab2" is lower than both "ac3" and "ab3". You cannot conclude anything regarding "ac3" and "ab3" with only these examples.
This result is expected since b comes before c in the alphabet (so "ab2" < "ac3") and 2 comes before 3 (so "ab2" < "ab3"): Java sorts Strings lexicographically.
Videos
From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:
String str1 = "hello";String str2 = "olleh";System.out.println(str1.compareTo(str2)); // 7
The general contract of Comparable.compareTo(o) is to return
- a positive integer if this is greater than the other object.
- a negative integer if this is lower than the other object.
- 0 if this is equals to the other object.
In your example "ab2".compareTo("ac3") == -1 and "ab2".compareTo("ab3") == -1 only means that "ab2" is lower than both "ac3" and "ab3". You cannot conclude anything regarding "ac3" and "ab3" with only these examples.
This result is expected since b comes before c in the alphabet (so "ab2" < "ac3") and 2 comes before 3 (so "ab2" < "ab3"): Java sorts Strings lexicographically.
compareTo for Strings returns -1 if the first String (the one for which the method is called) comes before the second String (the method's argument) in lexicographical order. "ab2" comes before "ab3" (since the first two characters are equal and 2 comes before 3) and also before "ac3" (since the first character is equal and b comes before c), so both comparisons return -1.
You seem to have misunderstood what is meant by "lexicographic order".
Since c comes after b, s1 is considered bigger than s2!
Think of letters as numbers. a is 1, b is 2, c is 3, and so on. 2 comes after 1, so 2 is bigger than 1.
So comparing bb and bc is just like comparing 22 and 23. Obviously, 23 is bigger.
Assuming below is the below program as per your code snippet:
Copypackage devsought;
public class JavaStringCompareTo {
public static void main(String... args) {
String s1="bc";
String s2="bb";
System.out.println(s1.compareTo(s2));
}
}
The output is 1
Explanation:
| b (index 0) | c (index 1) |
|---|---|
| b (index 0) | b (index 1) |
At index 1(second position),we have two characters ‘c’(unicode value \u0063,Decimal value 99) and ‘b’(unicode value \u0062 ,Decimal value 98).The difference 99-98=1 is returned and printed on the console. Check out for more examples on java String compareTo method