What does compareTo() actually return?

后端 未结 4 1824
感动是毒
感动是毒 2021-01-20 16:53

The compareTo() method in Java returns a value greater/equal/less than 0 and i know that. However, the value itself is my question. What is the difference betwe

相关标签:
4条回答
  • 2021-01-20 16:56

    If you take closer look at the source code for String#compareTo(String), you can see that the exact results are ambiguous.

    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;
    
        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }
    

    In most cases (i.e. a difference in the characters of both strings) it will return the integer difference of the char values of the first differing characters. Otherwise it will return the difference of the lengths of both strings.

    The interpretation of the return value beyond = 0, > 0 and < 0 should be of no concern in practice, since the implementation is allowed to change at any time if the contract of Comparable<T>#compareTo(T) is kept:

    Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

    Source: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-

    0 讨论(0)
  • 2021-01-20 16:56

    The exact value does not matter - all that the Comparable (as well as Comparator) interface cares about is whether the value is negative, zero or positive.

    This is to make things simple for implementations of the interface. When implementing it, you may choose to return the basic -1, 0 or 1 (this is usual if the comparison relies on evaluating some conditions), or you may use any arbitrary negative or positive value if it suits you better - e.g. you can compare two integers by returning this.i - other.i.


    In your particular given example, my guess would be:

    • -1 is difference in the third letter's code point: 'l' - 'm' == -1
    • -5 is difference in the first letter's code point: 'h' - 'm' == -5

    But the important part is that you shall not rely on it to be that way - it's an implementation detail, and according to Comparable's contract any negative value shall be treated the same ("less than").

    0 讨论(0)
  • 2021-01-20 17:04

    compareTo() returns the difference of first unmatched character in the two compared strings. If no unmatch is found, and one string comes out as shorter than other one, then the length difference is returned.

    "hello".compareTo("meklo") = 'h' - 'm'  = -5
     ^                 ^
    and 
    
    "hello".compareTo("hemlo") = 'l' - 'm'  = -1
       ^                 ^
    

    As a side note: Non-zero values are mostly considered as true inside conditional statements. So, compareTo can simply return these non-zero values instead of processing them into 1(small optimisation).

    0 讨论(0)
  • 2021-01-20 17:12

    https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

    This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

    this.charAt(k)-anotherString.charAt(k)

    If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

    this.length()-anotherString.length()

    0 讨论(0)
提交回复
热议问题