java的String中的compareTo方法源码

private final char value[];//value是字符数组
 
 
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; //返回ASC码之差,码越大,返回为正值;ASC码小,则返回负值;
            }
            k++;
        }
        return len1 - len2;  //若前面的字符串都相同,则字符串长的为大
    }


compareTo方法依次字符串中的字符的ASC码比较比是

你可能感兴趣的:(java,String,compareTo)