本文共 1726 字,大约阅读时间需要 5 分钟。
StringBuffer默认容量大小是16个字符。
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);; }}
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()); }}
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()); }}
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()); }}
转载地址:http://lejbx.baihongyu.com/