A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

今天问老师,源代码要怎样看,怎样学,才能有助于提高自己的编程能力?飞哥就让我回来分析这个问题!
先说String类
1.This class is implemented using a char[]
说明这个类是由char数组实现的,既然是数组,数组的长度赋值一次就不能改了
2.
[AppleScript] 纯文本查看 复制代码
    public String concat(String string) {
        if (string.count > 0 && count > 0) {
            char[] buffer = new char[count + string.count];
            System.arraycopy(value, offset, buffer, 0, count);
            System.arraycopy(string.value, string.offset, buffer, count, string.count);
            return new String(0, buffer.length, buffer);
        }
        return count == 0 ? string : this;
    }

这里返回一个新的String,说明不能更改其数据结构,只能创建一个新的数组
再来看StringBuffer
1.appent(String) 的源码
[AppleScript] 纯文本查看 复制代码
    /**
     * Adds the specified string to the end of this buffer.
     * <p>
     * If the specified string is {@code null} the string {@code "null"} is
     * appended, otherwise the contents of the specified string is appended.
     *
     * @param string
     *            the string to append (may be null).
     * @return this StringBuffer.
     */
    public synchronized StringBuffer append(String string) {
        append0(string);
        return this;
    }

返回的是当前StringBuffer,并没有创建一个新对象出来

大家还有什么想法,可以更加充分的证明问题的证伪性,欢迎拍砖

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马