public class Demo {
public static void main(String[] args) {
String s1 = "hello";
s1 += "World";
s1 += "我爱Java";
System.out.println(s1);
StringBuffer buf = new StringBuffer();
buf.append(true);
buf.append(10);
buf.append(3.14);
buf.append("hello");
buf.append("world");
buf.append("我爱Java");
// String s = "true" + "10" + "3.14" + "hello" + "world" + "我爱Java";
System.out.println(buf.toString());
StringBuffer buf2 = new StringBuffer();
StringBuilder bil = new StringBuilder();
long start = System.currentTimeMillis();
for(int i = 0;i < 10000; i++){
for(int j = 0; j < 5000 ; j++){
// buf2.append(i);
bil.append(i);
}
}
long end = System.currentTimeMillis();
System.out.println("buffer用时:" + (end - start) + " 毫秒!");//2532毫秒
System.out.println("Builder用时:" + (end - start) + " 毫秒!");//1406毫秒
}
}
StringBuffer使用append()方法添加字符串是,比如:
StringBuffer buf = new StringBuffer();
buf.append(true);
buf.append(10);
buf.append(3.14);
buf.append("hello");
buf.append("world");
buf.append("我爱Java");
对象buf的引用是一致固定不变的吗?它追加的字符串需要先在方法区中的常量池生成吗? |
|