今天问老师,源代码要怎样看,怎样学,才能有助于提高自己的编程能力?飞哥就让我回来分析这个问题!
先说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,并没有创建一个新对象出来
大家还有什么想法,可以更加充分的证明问题的证伪性,欢迎拍砖 |