Stringbuffer,Stringbuilder when to use? [duplicate]

懵懂的女人 提交于 2019-12-24 09:20:10

问题


Possible Duplicate:
StringBuilder and StringBuffer in Java

Criteria to choose among StringBuffer and StringBuilder


回答1:


If you're definitely using Java 5 or higher, and you definitely don't need to share the object between threads (I can't remember ever needing to do so), StringBuilder is a better bet.

Basically you should almost always use StringBuilder when you can, to avoid pointless synchronization. Admittedly the way most VMs handle uncontended synchronization is extremely efficient, but if you don't need it in the first place...




回答2:


StringBuilder methods are not synchronized, so when you are not concerned with multithreading part you can use it, as it would be fast.

StringBuffer on the other hand have all its method synchronized, and hence is thread safe.

You can go through this post: - Difference between StringBuilder and StringBuffer



来源:https://stackoverflow.com/questions/12961254/stringbuffer-stringbuilder-when-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!