Difference between StringBuilder and StringBuffer

后端 未结 30 2478
独厮守ぢ
独厮守ぢ 2020-11-21 15:06

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?

30条回答
  •  無奈伤痛
    2020-11-21 15:31

    Basically, StringBuffer methods are synchronized while StringBuilder are not.

    The operations are "almost" the same, but using synchronized methods in a single thread is overkill.

    That's pretty much about it.

    Quote from StringBuilder API:

    This class [StringBuilder] provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

    So it was made to substitute it.

    The same happened with Vector and ArrayList.

提交回复
热议问题