2016-07-24
1,StringBuffer简介
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());
StringBuffer sb2=new StringBuffer("abc");
System.out.println(sb2.capacity());
StringBuffer sb3=new StringBuffer(100);
System.out.println(sb3.capacity());
2,StringBuffer类的方法
线程安全的。
package com.java1995; public class TestStringBuffer { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); System.out.println(sb.capacity()); StringBuffer sb2 = new StringBuffer("abc"); System.out.println(sb2.capacity()); StringBuffer sb3 = new StringBuffer(100); System.out.println(sb3.capacity()); StringBuffer sb4 = new StringBuffer("Hello"); sb4.append(","); sb4.append("world!\n"); sb4.append("welcome to "); sb4.append("www.java1995.com"); System.out.println(sb4.toString()); } }
package com.java1995; public class TestStringBuffer2 { public static void main(String[] args) { StringBuffer sb = new StringBuffer("helloworld"); System.out.println(sb); for (int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); c = (char) (c - 32); sb.setCharAt(i, c); } System.out.println(sb); } }
package com.java1995; import java.util.Date; public class TestStringBuffer3 { public static void main(String[] args) { // System.out.println(new Date()); // String str=""; // for(int i=0;i<100000;i++){ // str+=i; // } // System.out.println(new Date()); // 18秒 System.out.println(new Date()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < 100000; i++) { sb.append(i); } System.out.println(new Date()); } }
【参考资料】