StringBuffer的解读(二)
StringBuffer默认容量大小是16个字符。
- StringBuffer后面append NULL的测试,代码如下:
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("the");
System.out.println("sb is: " + sb);;
String str = null;
sb.append(str);
System.out.println("sb is: " + sb);;
}
}
- StringBuffer后面append字符串+原先字符数组<容量的测试,代码如下:
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("the");
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
sb.append(" redpig");
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
}
}
- StringBuffer后面append字符串+原先字符数组>容量,且<容量x2+2的测试,代码如下:
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("the");
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
sb.append(" redpig is writting java programs");
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
}
}
- StringBuffer后面append字符串+原先字符数组>容量x2+2的测试,代码如下:
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("the");//original length is 3
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
sb.append(" redpig is writting java programs for fun");//string's length is 33
System.out.println("sb.length: " + sb.length());
System.out.println("sb.capacity: " + sb.capacity());
}
}