String类的compareTo方法的源码解析

-----------------------------------------------------------------------------
String类的compareTo方法的源码如下:

class String {
......
    private final char value[]; // 数组常量表示数组大小(长度)不能变。
      
    // 在底层会有一个转换,直接将字符串自动转换为一个字符数组。
      
    public int compareTo(String anotherString) {
        // this --> s1 --> "hello"
        // anotherString --> s2 --> "hel"
      
        int len1 = value.length;                 // this.value.length --> s1.toCharArray().length --> 5
        int len2 = anotherString.value.length;   // s2.value.length --> s2.toCharArray().length --> 3
        int lim = Math.min(len1, len2);          // Math.min(5,3); --> lim = 3;
        char v1[] = value; // value --> s1.toCharArray()
        char v2[] = anotherString.value;
            
        // char v1[] = {'h','e','l','l','o'};
        // char v2[] = {'h','e','l'};

        int k = 0;
        while (k < lim) {
            char c1 = v1[k]; // c1 = 'h','e','l'
            char c2 = v2[k]; // c2 = 'h','e','l'
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2; // 5 - 3 = 2;
    }
......
}

-----------------------------------------------------------------------------
示例代码:

  String s1 = "hello";
  String s2 = "hel";
  System.out.println(s1.compareTo(s2)); // 2
-----------------------------------------------------------------------------

posted @ 2018-02-27 23:01  黑泽君  阅读(341)  评论(0编辑  收藏  举报