public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println("sb:" + sb);
System.out.println("sb:" + sb.length());
System.out.println("sb:" + sb.capacity());
StringBuffer sb2 = new StringBuffer("hello");
System.out.println("sb2:" + sb2);
System.out.println("sb2:"+sb2.length());
System.out.println("sb2:"+sb2.capacity());
System.out.println("******************************");
sb.append("helloworld");
System.out.println("sb:" + sb);
System.out.println("sb:" + sb.length());
System.out.println("sb:" + sb.capacity());
System.out.println("******************************");
sb.append("helloworld");
System.out.println("sb:" + sb);
System.out.println("sb:" + sb.length());
System.out.println("sb:" + sb.capacity());
sb.append("hello");/
sb.append("helloworld");
System.out.println("sb:" + sb);
System.out.println("sb:" + sb.length());
System.out.println("sb:" + sb.capacity());
}
}
这里面是固定的只要添加,如果字符串长度超过容量,则添加后的字符串的容量都会变成2(以前容量+1),以保证容量是始终增加的.34=2*(16+1),70=2*(34+2).
|