String中各方法多数情况下返回新的String对象

(1)subString是否始终返回一个新串?

  事实并非如此,subString只有在截取的子串是真子串(真子串指子串与原串不相同)时才会返回一个新声明的子串。当截取的是整个字符串时,则返回自身,具体可参考源码:

  subString方法用来截取子串,方法的使用有两种形式String subString(int begin)和String subString(int begin, int end)。具体可参考JDK文档。

public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }
View Code

(2)replace方法是否始终返回一个新串?

  在一般情况下是这样,但如果要替代的字符与新字符一样的话,则返回自身:

public String replace(char oldChar, char newChar) {
        if (oldChar != newChar) {
            int len = value.length;
            int i = -1;
            char[] val = value; /* avoid getfield opcode */

            while (++i < len) {
                if (val[i] == oldChar) {
                    break;
                }
            }
            if (i < len) {
                char buf[] = new char[len];
                for (int j = 0; j < i; j++) {
                    buf[j] = val[j];
                }
                while (i < len) {
                    char c = val[i];
                    buf[i] = (c == oldChar) ? newChar : c;
                    i++;
                }
                return new String(buf, true);
            }
        }
        return this;
    }
View Code

(3)toCharArray方法返回String对象内置的字符数组?

  并不是这样,方法返回的是一个崭新的字符数组,且空间分配在堆空间。所以对该方法返回的字符数组进行修改,不会对String对象产生任何影响。

public char[] toCharArray() {
        // Cannot use Arrays.copyOf because of class initialization order issues
        char result[] = new char[value.length];
        System.arraycopy(value, 0, result, 0, value.length);
        return result;
    }
View Code

(4)toString方法返回的是一个新的String对象吗?

  这个不是的,该方法返回的就是自身。

public String toString() {
        return this;
    }
View Code

 (5)trim方法始终返回一个新的String对象吗?

  并不总是这样,首先trim方法是返回String对象的一个副本,该副本去除了原来字符串中的首部和尾部空白。但如果String对象首部和尾部没有空白的话,则返回自身。否则调用subString方法返回一个崭新的String对象(真子串,subString方法中说过)。

public String trim() {
        int len = value.length;
        int st = 0;
        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[st] <= ' ')) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= ' ')) {
            len--;
        }
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }
View Code
原文地址:https://www.cnblogs.com/liujinyao/p/4733446.html