There is no much difference between string buffer and string
builder.only thing is all the methods available in string
buffer class are synchronized methods.and the method
available in string builder class are non-synchronized methods.
If we want thread safety then we have to go for string
buffer ,but performance goes down.if we don't want thread
safety the we can go for string builder
..............................................................
Both are mutable ,that means we can change value of the
instance variable using append,insert and ...
But only one difference when you go for the multi-thread
concept you use StringBuffer,Because it is thread safe ,that
means, if one thread access this stringbuffer variable on
the time another thread cant access this stringbuffer,so
stringbuffer is slowest performance compare to stringbuilder.
..............................................................
StringBuffer
came first. Sun was concerned with correctness under all conditions, so they made it synchronized to make it thread-safe just in case.StringBuilder
came later. Most of the uses of StringBuffer
were single-thread and unnecessarily paying the cost of the synchronization.Since
StringBuilder
is a drop-in replacement for StringBuilder
without the synchronization, there would not be differences between any examples.Simply use
StringBuilder
unless you really are trying to share a buffer between threads.If you are trying to share between threads, either use
StringBuffer
or consider whether higher-level synchronization is necessary, e.g. synchronizing the methods that use the buffer or synchronizing on the class that has the buffer as an instance var.
0 comments:
Post a Comment