猿问

为什么Java String源码方法regionMatches()不调用其重载实现?

String源码里的这两个方法如下:

    public boolean regionMatches(int toffset, String other, int ooffset,
                int len) {
            char ta[] = value;
            int to = toffset;
            char pa[] = other.value;
            int po = ooffset;
            // Note: toffset, ooffset, or len might be near -1>>>1.
            if ((ooffset < 0) || (toffset < 0)
                    || (toffset > (long)value.length - len)
                    || (ooffset > (long)other.value.length - len)) {
                return false;
            }
            while (len-- > 0) {
                if (ta[to++] != pa[po++]) {
                    return false;
                }
            }
            return true;
        }
        
    

     public boolean regionMatches(boolean ignoreCase, int toffset,
            String other, int ooffset, int len) {
        char ta[] = value;
        int to = toffset;
        char pa[] = other.value;
        int po = ooffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0)
                || (toffset > (long)value.length - len)
                || (ooffset > (long)other.value.length - len)) {
            return false;
        }
        while (len-- > 0) {
            char c1 = ta[to++];
            char c2 = pa[po++];
            if (c1 == c2) {
                continue;
            }
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            }
            return false;
        }
        return true;
    }

但是,明显可以这么写

    public boolean regionMatches(int toffset, String other, int ooffset,int len) {
        return regionMatches(false,toffset, other, ooffset, len);
    }

但是jdk8里还是这样分开实现的,是处于什么其他考虑吗?
(话说string里类似的重复实现还有不少)

呼啦一阵风
浏览 391回答 1
1回答

慕少森

测试程序: String s1 = "oshagoeutno;shfsSFSDAFsfjgpdhsf;AJofdi1248938jdsgjJDJFSDoghp79874hgajfJDy9(FS*U9prhgdsjfdghnsdfasf"; String s2 = "oshagoeutno;shfsSFSDAFsfjgpdhsf;AJofdi1248938jfogheyjJDJFSDoghp79874hgajfJDy9(FS*U9prhgdsjfdghnsdfasf"; long start = System.nanoTime(); for (int i = 0; i < 10_000_000; i++) { s1.regionMatches(5, s2, 5, 65); } long end = System.nanoTime(); System.out.println("regionMatches(): " + (end - start)); start = System.nanoTime(); for (int i = 0; i < 10_000_000; i++) { s1.regionMatches(false, 5, s2, 5, 65); } end = System.nanoTime(); System.out.println("regionMatches(false): " + (end - start)); 结果: regionMatches(): 633992574 regionMatches(false): 805662114 第一个比第二个稍微快一些。
随时随地看视频慕课网APP

相关分类

Java
我要回答