StringBuffer的解读(二)

redpigX 2019-03-12

java 测试 string static class void test 数组

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());
    }
}
登录 后评论
下一篇
我是小助手
31877人浏览
2019-07-31
相关推荐
解读HTTP包
386人浏览
2017-11-24 14:43:00
解读HTTP包
474人浏览
2017-11-16 00:09:00
StringBuffer的解读(一)
354人浏览
2019-03-12 15:22:43
java 生成 csv文件
346人浏览
2015-09-11 15:14:00
spring-session源码解读-5
1612人浏览
2015-12-15 19:58:00
2018-05-30 第二十四天
622人浏览
2018-05-30 22:35:26
06 - JavaSE之常用类
458人浏览
2018-01-07 17:01:00
Java杂记17—String全面解析
842人浏览
2018-05-29 09:45:00
0
0
0
366