4

Possible Duplicate:
StringBuilder and StringBuffer in Java

what is the difference between StringBuilder and Stringbuffer?

1
  • StringBuilder is a bit faster. Commented May 27, 2010 at 12:51

2 Answers 2

5

Some methods in StringBuffer are synchronized while StringBuilder is not thread-safe - and faster.

Rule of a thumb - use StringBuilder unless you have a use case, where a StringBuilder is used by more then one Thread (which would be a very rare case).

Sign up to request clarification or add additional context in comments.

Comments

4

Taken from the javadoc of StringBuffer:

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, {@link StringBuilder}. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Basically StringBuffer can be used by multiple threads at the same time, since it's synchronized, but that also makes it a bit slower than StringBuilder which can only be used by one thread at a time.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.