StringBuffer&StringBuilder不可变? 大家都说String不可变的原因是因为JDK源码中String类声明为final的原因.那么我有一个问题:既然String类声明为fianl就不可变.那么StringBuffer类和StringBuilder也是被声明为final的.为什么他俩又可以变呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大家都说String不可变的原因是因为JDK源码中String类声明为final的原因.-----------------------------------------------------------------------------final保证的只是不可被继承而已。String那特殊而变态的不变是String自己的实现造成的:StringBuffer和StringBuilder有append之类改变自己的方法,String所有改变自己的方法其实都new出了新类( ̄(工) ̄) 包括+操作,jvm里也通过StringBuffer和StringBuilder处理,最后new一个新的 关于String的不可改变的分析,以及与StringBuilder的区别。理解错误,String不可变是因为1 类自身是final的2 类里面的内容也是final的。比如最重要的保存字符串的部分 private final char value[];而StringBuilder呢? public StringBuilder() { super(16); }在 AbstractStringBuilder 里面是 char value[]; AbstractStringBuilder(int capacity) { value = new char[capacity]; }可见,这个char是可以变化的。通过一个 expandCapacity的方法进行扩充容量。这就是区别 //String 类中 public String toUpperCase(Locale locale) { ...... ...... return new String(0, count+resultOffset, result); }//StringBuffer类中public synchronized StringBuffer append(String str) { super.append(str); return this; }//StringBuilder类中public StringBuilder append(String str) { super.append(str); return this; }是这样阿?~~ public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); }public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = count; int i = -1; char[] val = value; /* avoid getfield opcode */ int off = offset; /* avoid getfield opcode */ while (++i < len) { if (val[off + i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0 ; j < i ; j++) { buf[j] = val[off+j]; } while (i < len) { char c = val[off + i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(0, len, buf); } } return this; }每个函数都不一样啊.?有的返回new的新对象,有的是对象本身. public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = count; int i = -1; char[] val = value; /* avoid getfield opcode */ int off = offset; /* avoid getfield opcode */ while (++i < len) { if (val[off + i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0 ; j < i ; j++) { buf[j] = val[off+j]; } while (i < len) { char c = val[off + i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(0, len, buf); } } return this; } public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); }每个函数都不一样吖?!有的是返回new的新对象 ,有的是返回this对象本身 robocode 线程 求救 谁有在linux下jsp上传图片并生成缩位图或者加水印 做成功过呢? Too many open files 关于new package加载的问题 问一个极其傻瓜的问题 晕:当一个类被加载后,如果这个类含有另一个类的方法,那它也会一起加载吗? 关于sr = new StringReader(xmlStr);的问题?????? 奇怪的问题:找不到驱动(急!!) 认证试题,没看懂,大侠助我,谢谢! where can i download WebSphere4.0 ? 网络编程 多线程 时钟 JAVA连接数据库时,为什么输出的u 和p 值都是不正确?
-----------------------------------------------------------------------------
final保证的只是不可被继承而已。
String那特殊而变态的不变是String自己的实现造成的:
StringBuffer和StringBuilder有append之类改变自己的方法,String所有改变自己的方法其实都new出了新类( ̄(工) ̄)
1 类自身是final的
2 类里面的内容也是final的。比如最重要的保存字符串的部分
private final char value[];而StringBuilder呢?
public StringBuilder() {
super(16);
}
在 AbstractStringBuilder 里面是
char value[];
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}可见,这个char是可以变化的。通过一个 expandCapacity的方法进行扩充容量。
这就是区别
//String 类中
public String toUpperCase(Locale locale) {
......
......
return new String(0, count+resultOffset, result);
}
//StringBuffer类中
public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}
//StringBuilder类中
public StringBuilder append(String str) {
super.append(str);
return this;
}是这样阿?~~
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen];
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = count;
int i = -1;
char[] val = value; /* avoid getfield opcode */
int off = offset; /* avoid getfield opcode */ while (++i < len) {
if (val[off + i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0 ; j < i ; j++) {
buf[j] = val[off+j];
}
while (i < len) {
char c = val[off + i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(0, len, buf);
}
}
return this;
}
每个函数都不一样啊.?有的返回new的新对象,有的是对象本身.
if (oldChar != newChar) {
int len = count;
int i = -1;
char[] val = value; /* avoid getfield opcode */
int off = offset; /* avoid getfield opcode */ while (++i < len) {
if (val[off + i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0 ; j < i ; j++) {
buf[j] = val[off+j];
}
while (i < len) {
char c = val[off + i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(0, len, buf);
}
}
return this;
}
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen];
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}
每个函数都不一样吖?!有的是返回new的新对象 ,有的是返回this对象本身