阅读Java源码之String#compareTo

阅读Java源码之String#compareTo

困而学,学而知

 /**
 * Compares two strings lexicographically.
     * The comparison is based on the Unicode value of each character in
     * the strings. The character sequence represented by this
     * {@code String} object is compared lexicographically to the
     * character sequence represented by the argument string. The result is
     * a negative integer if this {@code String} object
     * lexicographically precedes the argument string. The result is a
     * positive integer if this {@code String} object lexicographically
     * follows the argument string. The result is zero if the strings
     * are equal; {@code compareTo} returns {@code 0} exactly when
     * the {@link #equals(Object)} method would return {@code true}.
     * <p>
     * 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 <i>k</i> be the smallest such index; then the string
     * whose character at position <i>k</i> has the smaller value, as
     * determined by using the &lt; operator, lexicographically precedes the
     * other string. In this case, {@code compareTo} returns the
     * difference of the two character values at position {@code k} in
     * the two string -- that is, the value:
     * <blockquote><pre>
     * this.charAt(k)-anotherString.charAt(k)
     * </pre></blockquote>
     * If there is no index position at which they differ, then the shorter
     * string lexicographically precedes the longer string. In this case,
     * {@code compareTo} returns the difference of the lengths of the
     * strings -- that is, the value:
     * <blockquote><pre>
     * this.length()-anotherString.length()
     * </pre></blockquote>
     *
     * @param   anotherString   the {@code String} to be compared.
     * @return  the value {@code 0} if the argument string is equal to
     *          this string; a value less than {@code 0} if this string
     *          is lexicographically less than the string argument; and a
     *          value greater than {@code 0} if this string is
     *          lexicographically greater than the string argument.
     */ 
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;
}

注释解释

  CompareTo按照字典顺序(lexicographically)比较两个字符串。基于字符串中的每个字符的Unicode值。当前(this)String对象字符序列与给定的参数String字符序列通过字典顺序作比较。如果当前String对象字典顺序大于参数String字典顺序,就返回正整数; 如果当前String对象字段顺序小于参数String字典顺序,就返回负整数;相等(equal(Object))就返回0。

  定义字典顺序。这里是如何定义字典顺序。如果两个字符串不同,意味着它们在某个索引处具有不同的字符, 或者它们的长度不同,也或两种都有。如果它们在一个或多个索引处有不同的字符。假设 K 是这类索引的最小值;则在位置 K 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo 返回这两个字符串在位置 K处两个char 值的差,即值:

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

  如果没有字符不同的索引位置,则较短字符串的字典顺序在较长字符串之前。在这种情况下,k 返回这两个字符串长度的差,即值:

this.length()-anotherString.length()

源码

 1. 取出两个字符串的长度len1, len2  2. 计算两个长度的最小值lim  3. 比较较小的长度内,两个字符串是否相等     1. 若不相等,就返回该位置字符的ASCII码相减后的值。  4. 若相等,就返回两个字符串的长度差值返回。

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://baozi.fun/archives/阅读Java源码之String的compareTo

Buy me a cup of coffee ☕.