解释:
StringBuffer( String s ); //除了按照s的大小分配空间外,再分配16个字符的缓冲区
你的StringBuffer是用字符构造的,
"it is a far,far better thing that i do!"的长度是39另外再分配16个字符共55,
所以字符串缓冲区的容量为55,
后边你改的是它的长度,改不了容量的(除非你的字符串长度超过55....)所以输出的容量仍然是55.
要是写成这样的话:
StringBuffer sb=new StringBuffer("it is a far,far better thing that i do!");
System.out.println("sb="+sb);
System.out.println("sb.length()="+sb.length());
System.out.println("sb.capacity()="+sb.capacity());
sb.append("dasdfghjklkjhgfds");
System.out.println("sb="+sb);
System.out.println("sb.length()="+sb.length());
System.out.println("sb.capacity()="+sb.capacity());
输出结果是:
sb=it is a far,far better thing that i do!
sb.length()=39
sb.capacity()=55
sb=it is a far,far better thing that i do!dasdfghjklkjhgfds
sb.length()=56
sb.capacity()=112
当超过55的话它的缓冲区容量就编程56的2倍了.. |