1.三者在執(zhí)行速度方面的比較:StringBuilder > StringBuffer > String
2.String <(StringBuffer,StringBuilder)的原因
String:字符串常量
StringBuffer:字符串變量
StringBuilder:字符串變量
3.StringBuilder與 StringBuffer
StringBuilder:線程非安全的
StringBuffer:線程安全的
當(dāng)我們在字符串緩沖去被多個(gè)線程使用是荣暮,JVM不能保證StringBuilder的操作是安全的进副,雖然他的速度最快,但是可以保證StringBuffer是可以正確操作的悬槽。當(dāng)然大多數(shù)情況下就是我們是在單線程下進(jìn)行的操作怀吻,所以大多數(shù)情況下是建議用StringBuilder而不用StringBuffer的,就是速度的原因初婆。
對于三者使用的總結(jié): 1.如果要操作少量的數(shù)據(jù)用 = String
2.單線程操作字符串緩沖區(qū) 下操作大量數(shù)據(jù) = StringBuilder
3.多線程操作字符串緩沖區(qū) 下操作大量數(shù)據(jù) = StringBuffer
參考https://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html
關(guān)于速度
http://blog.csdn.net/mad1989/article/details/26389541
public class StringTest {
public static String BASEINFO = "Mr.Y";
public static final int COUNT = 2000000;
/**
* 執(zhí)行一項(xiàng)String賦值測試
*/
public static void doStringTest() {
String str = new String(BASEINFO);
long starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT / 100; i++) {
str = str + "miss";
}
long endtime = System.currentTimeMillis();
System.out.println((endtime - starttime)
+ " millis has costed when used String.");
}
/**
* 執(zhí)行一項(xiàng)StringBuffer賦值測試
*/
public static void doStringBufferTest() {
StringBuffer sb = new StringBuffer(BASEINFO);
long starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
sb = sb.append("miss");
}
long endtime = System.currentTimeMillis();
System.out.println((endtime - starttime)
+ " millis has costed when used StringBuffer.");
}
/**
* 執(zhí)行一項(xiàng)StringBuilder賦值測試
*/
public static void doStringBuilderTest() {
StringBuilder sb = new StringBuilder(BASEINFO);
long starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
sb = sb.append("miss");
}
long endtime = System.currentTimeMillis();
System.out.println((endtime - starttime)
+ " millis has costed when used StringBuilder.");
}
/**
* 測試StringBuffer遍歷賦值結(jié)果
*
* @param mlist
*/
public static void doStringBufferListTest(List<String> mlist) {
StringBuffer sb = new StringBuffer();
long starttime = System.currentTimeMillis();
for (String string : mlist) {
sb.append(string);
}
long endtime = System.currentTimeMillis();
System.out.println(sb.toString() + "buffer cost:"
+ (endtime - starttime) + " millis");
}
/**
* 測試StringBuilder迭代賦值結(jié)果
*
* @param mlist
*/
public static void doStringBuilderListTest(List<String> mlist) {
StringBuilder sb = new StringBuilder();
long starttime = System.currentTimeMillis();
for (Iterator<String> iterator = mlist.iterator(); iterator.hasNext();) {
sb.append(iterator.next());
}
long endtime = System.currentTimeMillis();
System.out.println(sb.toString() + "builder cost:"
+ (endtime - starttime) + " millis");
}
public static void main(String[] args) {
doStringTest();
doStringBufferTest();
doStringBuilderTest();
List<String> list = new ArrayList<String>();
list.add(" I ");
list.add(" like ");
list.add(" BeiJing ");
list.add(" tian ");
list.add(" an ");
list.add(" men ");
list.add(" . ");
doStringBufferListTest(list);
doStringBuilderListTest(list);
}
}