码迷,mamicode.com
首页 > 其他好文 > 详细

String, StringBuffer, and StringBuilder

时间:2015-04-10 11:32:47      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

关于这三者的区别已经是老话题了,上stackoverflow看到目前最中肯简洁的回答


http://stackoverflow.com/a/2971343



Mutability Difference:

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values.


Thread-Safety Difference:


The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs to be run only in a single thread then it is better to use StringBuilderStringBuilder is more efficient than StringBuffer.


Situations:


  • If your string is not going to change use a String class because a String object is immutable.
  • If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
  • If your string can change, and will be accessed from multiple threads, use a StringBufferbecause StringBuffer is synchronous so you have thread-safety.

String, StringBuffer, and StringBuilder

标签:

原文地址:http://blog.csdn.net/chziroy/article/details/44975477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!