StringBuilder StringBuffer

来源:互联网 发布:网站推广软件skycc 编辑:程序博客网 时间:2024/05/16 05:34

StringBuffer

源码中定义如下:

A modifiable CharSequence for use in creating strings, where all
accesses are synchronized. This class has mostly been replaced by
StringBuilder because this synchronization is rarely useful.

StringBuffer可修改,所有可访问方法都是同步的但是此方法几乎已经被StringBuilder替代了,因为同步方法很少使用到。

StringBuilder

A modifiable CharSequence for use in creating strings. This class is intended as a direct replacement of StringBuffer for non-concurrent use; unlike StringBuffer this class is not synchronized.

如果只是单线程使用的话,建议使用StringBuilder,因为它的速度比StringBuffer快。若考虑线程安全,建议使用StringBuffer。

0 0
原创粉丝点击