StringBuffer 与StringBuilder的应用场景
首先翻译JDK规格说明中StringBuffer类描述的一段话:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, {@link StringBuilder}. The <tt>StringBuilder</tt> class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
自从JDK5发布后,该类(StringBuffer)被补充了一个为在单线程中使用而设计的同等的类StringBuilder。StringBuilder通常要优先于该类被使用,因为它支持所有相同的操作,但速度更快,只是它没有执行任何同步。
结论:
对象A在单线程中使用,则选用StringBuilder A;
对象A被多线程使用,则选用StringBuffer A.