问一个stringbuffer的问题,求大牛帮忙解答

davidcoffee 2009-11-27 08:05:39
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怎么算的~谢谢大牛们啦~
...全文
65 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
HappyKeKe 2009-11-27
  • 打赏
  • 举报
回复
原来这样
若鱼1919 2009-11-27
  • 打赏
  • 举报
回复
看源代码就知道了:
(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);
}
...................
}
若鱼1919 2009-11-27
  • 打赏
  • 举报
回复
看源代码就知道了:
(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);
}
...................
}
bawgiitx 2009-11-27
  • 打赏
  • 举报
回复

/* 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);
}
lgm277531070 2009-11-27
  • 打赏
  • 举报
回复
不清楚 帮顶
不善^ 2009-11-27
  • 打赏
  • 举报
回复
不清楚 应该是超过过 随机 分配吧
呵呵 猜的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧