Re: Why can a final(!) StringBuffer can be appended?
On 17.11.2006 13:24, Chris Uppal wrote:
Robert Klemme wrote:
They changed that in 1.5. In 1.4 it was shared. The concept is know as
"copy on write".
They didn't change it, it's the same in 1.5 as in 1.4 (and, as far as I
remember) all the previous versions too.
I beg to differ. JDK 1.4.2-11 (copy on write)
StringBuffer:
public String toString() {
return new String(this);
}
final void setShared() { shared = true; }
final char[] getValue() { return value; }
String:
public String (StringBuffer buffer) {
synchronized(buffer) {
buffer.setShared();
this.value = buffer.getValue();
this.offset = 0;
this.count = buffer.length();
}
}
JDK 1.5.0-6 (always copy)
StringBuffer:
public synchronized String toString() {
return new String(value, 0, count);
}
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
char[] v = new char[count];
System.arraycopy(value, offset, v, 0, count);
this.offset = 0;
this.count = count;
this.value = v;
}
Regards
robert