what is the difference between the StringBuffer vs StringBuilder Vs StringTokenizer on the internal implementation. when to use these . kindly waiting for the answer.
I am also going through the source code.
String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
Example of StringTokenizer Class Let's see an example of the StringTokenizer class that tokenizes a string "my name is khan" on the basis of whitespace. The above Java code, demonstrates the use of StringTokenizer class and its methods hasMoreTokens() and nextToken().
Because StringBuffer methods are synchronized whereas StringBuilder methods are not, there is a speed difference. StringBuffer is more secure to use than StringBuilder, but it is also slower due to the additional synchronization mechanisms.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With