您的位置:首页 > 产品设计 > UI/UE

String StringBuffer StringBuilder

2016-04-18 11:27 344 查看
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 
StringBuilder
StringBuilder
 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 
StringBuffer
because 
StringBuffer
 is
synchronous so you have thread-safety.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: