package String比较;
/*
* String 与StringBuffer比较
* String 不可变,一旦赋值,就不能被修改
* StringBuffer可变的字符串。
* StringBuffer的追加效率更高
*/ public class Test8 { public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abc";
str.replace("a", "b");
System.out.println(str);// abc
StringBuffer sbf = new StringBuffer("abc");
sbf.replace(0, 1, "de");
System.out.println(sbf);// debc String s1 = "chinasofti";
int time = 10000;
@SuppressWarnings("unused")
String tempstr = "";
long start1 = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
tempstr += s1;
}
long end1 = System.currentTimeMillis();
System.out.println("String:" + (end1 - start1) + "ms");// 876ms StringBuffer tempsbf = new StringBuffer();
long start2 = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
tempsbf.append(s1);
}
long end2 = System.currentTimeMillis();
System.out.println("StringBuffer:" + (end2 - start2) + "ms");// 1ms
} }
05-08 08:11