StringBuffer的解读(二)

  1. 云栖社区>
  2. 博客>
  3. 正文

StringBuffer的解读(二)

redpigX 2019-03-12 15:34:12 浏览385
展开阅读全文

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());
    }
}

网友评论

登录后评论
0/500
评论
redpigX
+ 关注