String StringBuffer StringBuilder的不同

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.


  • 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.
  1. String的值不能改变,其他两个都可以改变值。
  2. StringBuffer是线程安全的,多线程访问的时候用它。
  3. StringBuilder不是线程安全的,所以只有一个线程的时候用StringBuilder,效率还高。
Tagged on: ,


电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.