package String;public class capacity {
public static void main(String[] args) {
StringBuffer sbfSource1 = new StringBuffer(5);
StringBuffer sbfSource2 = new StringBuffer(5);
sbfSource1.append("you");
sbfSource2.append("youandme");
System.out.println("字符串缓冲区的容量为:" + sbfSource1.capacity());
System.out.println("字符串缓冲区的容量为:" + sbfSource2.capacity());
}
}
第一个容量由于没超出5所以是5,那第二个sbfSource2的缓冲区容量为什么是12怎么算的~谢谢大牛们啦~
public static void main(String[] args) {
StringBuffer sbfSource1 = new StringBuffer(5);
StringBuffer sbfSource2 = new StringBuffer(5);
sbfSource1.append("you");
sbfSource2.append("youandme");
System.out.println("字符串缓冲区的容量为:" + sbfSource1.capacity());
System.out.println("字符串缓冲区的容量为:" + sbfSource2.capacity());
}
}
第一个容量由于没超出5所以是5,那第二个sbfSource2的缓冲区容量为什么是12怎么算的~谢谢大牛们啦~
/* public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence类中
*/
public synchronized StringBuffer append(String str) {
super.append(str);////
return this;
}/////////abstract class AbstractStringBuilder中
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);/////////////
str.getChars(0, len, value, count);
count = newCount;
return this;
}
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;/////////这里(5+1)*2=12
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
(1)
public final class StringBuffer extends AbstractStringBuilder
implements java.io.Serializable, CharSequence{
public StringBuffer(int capacity) {
super(capacity);
}
public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}
...................
}(2)
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char value[];
/**
* The count is the number of characters used.
*/
int count;
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;//-->就是在这个地方(5+1)*2=12
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
...................
}
(1)
public final class StringBuffer extends AbstractStringBuilder
implements java.io.Serializable, CharSequence{
public StringBuffer(int capacity) {
super(capacity);
}
public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}
...................
}(2)
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char value[];
/**
* The count is the number of characters used.
*/
int count;
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;//-->就是在这个地方(5+1)*2=12
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
...................
}