读书人

StringBuffer 与StringBuilder的运用场

发布时间: 2012-11-05 09:35:11 作者: rapoo

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.


1 楼 carafe 2011-08-12 一般都用StringBuilder,缩小作用域就可以了

读书人网 >编程

热点推荐