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.

Community
  • 1
  • 1
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • 1
    Possible duplicate of [Difference between StringBuilder and StringBuffer](http://stackoverflow.com/questions/355089/difference-between-stringbuilder-and-stringbuffer) – Jess Bowers Jun 20 '16 at 13:33
  • See [this Answer](https://stackoverflow.com/a/31169001/642706) of mine for a nifty diagram I made as an overview of the string-related classes. – Basil Bourque Dec 18 '21 at 08:19

4 Answers4

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • `StringTokenizer`, while not deprecated, is also a "legacy class that is retained for compatibility reasons although its use is discouraged in new code". http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html – Henning Sep 24 '10 at 09:51
  • @Henning, thanks for noting - I updated my answer to include this. – Péter Török Sep 24 '10 at 09:53
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

jmj
  • 237,923
  • 42
  • 401
  • 438
4

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gadolin
  • 2,636
  • 3
  • 28
  • 33
  • Wouldn't it be simpler and clearer to say "StringBuilder is the non-synchronized version of StringBuffer"? – Péter Török Sep 24 '10 at 09:57
  • I am working a lot in java prior 1.5 where there was no StringBuilder, so my reference point is StringBuffer :-) – Gadolin Sep 24 '10 at 10:24
  • @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. – user207421 Sep 25 '10 at 02:43
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.

keshav84
  • 2,291
  • 5
  • 25
  • 34