String, StringBuffer, and StringBuilder
2015-04-10 10:01
369 查看
关于这三者的区别已经是老话题了,上stackoverflow看到目前最中肯简洁的回答
http://stackoverflow.com/a/2971343
Mutability Difference:
Stringis immutable,
if you try to alter their values, another object gets created, whereas
StringBufferand
StringBuilderare mutable so
they can change their values.
Thread-Safety Difference:
The difference between
StringBufferand
StringBuilderis
that
StringBufferis
thread-safe. So when the application needs to be run only in a single thread then it is better to use
StringBuilder.
StringBuilderis
more efficient than
StringBuffer.
Situations:
If your string is not going to change use a String class because a
Stringobject
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
StringBuilderis
good enough.
If your string can change, and will be accessed from multiple threads, use a
StringBufferbecause
StringBufferis
synchronous so you have thread-safety.
相关文章推荐
- String、StringBuffer、StringBuilder三者的区别
- String、StringBuffer与StringBuilder之间区别
- Java字符串(String_StringBuilder_StringBuffer)
- 深入源码剖析String,StringBuilder,StringBuffer
- String与StringBuffer,StringBuilder的区别
- Java String StringBuilder StringBuffer
- String,StringBuffer与StringBuilder的区别??
- Java核心知识点-String、StringBuilder和StringBuffer的特点
- java中String,StringBuffer和StringBuilder的区别
- java基础(一)String,StringBuffer,StringBuilder
- String与StringBuffer、StringBuilder
- String、StringBuffer与StringBuilder之间区别
- String与StringBuffer、StringBuilder的区别
- 深入java String拼接和StringBuffer、StringBuilder(分析源码)
- String,StringBuffer,StringBuilder
- String StringBuffer和StringBuilder异同
- java基础(二)------ String,StringBuffer,StringBuilder有什么区别?
- String, StringBuilder及StringBuffer的区别
- String, StringBuffer, StringBuilder三者区别
- String-vs-StringBuilder-vs-StringBuffer