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());
    }
}
登录 后评论
下一篇
阿里巴巴云原生小助手
762人浏览
2020-02-11
相关推荐
StringBuffer的解读(一)
683人浏览
2019-03-12 15:22:43
spring-session源码解读-5
1772人浏览
2015-12-15 19:58:00
org.springframework.core.styler包解读
1033人浏览
2017-05-17 13:12:39
java 生成 csv文件
511人浏览
2015-09-11 15:14:00
解读HTTP包
599人浏览
2017-11-16 00:09:00
解读HTTP包
479人浏览
2017-11-24 14:43:00
2018-05-30 第二十四天
804人浏览
2018-05-30 22:35:26
06 - JavaSE之常用类
623人浏览
2018-01-07 17:01:00
Java杂记17—String全面解析
1021人浏览
2018-05-29 09:45:00
0
0
0
523