三者的区别主要在两个方面:执行速度与线程安全方面
速度:StringBuilder > StringBuffer > String
为什么String的速度慢呢?String是字符常量,StringBuilder和StringBuffer都是字符变量
安全:StringBuffer是线程安全的
1、查看StringBuffer源码(部分)可见
* A thread-safe, mutable sequence of characters.
*
* //String buffers 是多线程安全的。
* String buffers are safe for use by multiple threads. The methods
* are synchronized where necessary so that all the operations on any
* particular instance behave as if they occur in some serial order
* that is consistent with the order of the method calls made by each of
* the individual threads involved.
同时,可以看到大量synchronized修饰的方法。
2、查看StringBuilder源码(部分)可见
//StringBuilder的实例在多线程中不安全
<p>Instances of {@code StringBuilder} are not safe for
* use by multiple threads. If such synchronization is required then it is
* recommended that {@link java.lang.StringBuffer} be used.
3、String
* Strings are constant; their values cannot be changed after they
* are created. String buffers support mutable strings.
* Because String objects are immutable they can be shared. For example:
*
* String str = "abc";
*
* is equivalent to:
*
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
*
* Here are some more examples of how strings can be used:
*
* System.out.println("abc");
* String cde = "cde";
* System.out.println("abc" + cde);
* String c = "abc".substring(2,3);
* String d = cde.substring(1, 2);
*
*
Java中的String,StringBuilder,StringBuffer三者的区别(详细描述):http://www.cnblogs.com/su-feng/p/6659064.html