9

what is the difference between the StringBuffer vs StringBuilder Vs StringTokenizer on the internal implementation. when to use these . kindly waiting for the answer.

Update:-

I am also going through the source code.

2

4 Answers 4

21

StringBuffer - introduced in JDK 1.0 - is thread safe (all of its methods are synchronized), while StringBuilder - since JDK 1.5 - is not. Thus it is recommended to use the latter under normal circumstances.

StringTokenizer is meant for a whole different purpose then the former two: cutting strings into pieces, rather than assembling. As @Henning noted, it is also "retired" since JDK 1.5 - it is recommended to use String.split instead.

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

2 Comments

StringTokenizer, while not deprecated, is also a "legacy class that is retained for compatibility reasons although its use is discouraged in new code". download.oracle.com/javase/6/docs/api/java/util/…
@Henning, thanks for noting - I updated my answer to include this.
9
  • StringBuffer is designed to be thread-safe and all public methods in StringBuffer are synchronized. StringBuilder does not handle thread-safety issue and none of its methods is synchronized.

  • StringBuilder has better performance than StringBuffer under most circumstances.

  • Use the new StringBuilder wherever possible.

Here is performance comparison of StringBuilder & StringBuffer

StringBuilder & StringBuffer Holds String where StringoTokeizer class allows an application to break a string into tokens .. So It is like odd one out

Comments

4

StringBuffer - is synchronized version of StringBuilder (introduced after its unsynchronized peer)

3 Comments

Wouldn't it be simpler and clearer to say "StringBuilder is the non-synchronized version of StringBuffer"?
I am working a lot in java prior 1.5 where there was no StringBuilder, so my reference point is StringBuffer :-)
@Peter Török: No. It is perfectly simple and clear the way it is, and also gives a historical perspective, which yours doesn't.
3

StringBuffer serves same purpose as StringBuilder except that StringBuffer is thread-safe.
StringTokenizer is used for splitting the string into tokens based on some delimiters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.