public class HelloWord{
public static void main(String[] args){
StringBuffer sb = new StringBuffer("abcdefgh");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(30);
System.out.println("set the length is 30");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(60);
System.out.println("set the length is 60");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(120);
System.out.println("set the length is 120");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
}
}
结果是
the length is 8
the capacity is 24
set the length is 30
the length is 30
the capacity is 50
set the length is 60
the length is 60
the capacity is 102
set the length is 120
the length is 120
the capacity is 206
这是为什么呢