Suppose our application have only one thread. and we are using StringBuffer
then what is the problem?
I mean if StringBuffer
can handle multiple threads through synchronization, what is the problem to work with single thread?
Why use StringBuilder
instead?
Manish, although there is just one thread operating on your StringBuffer instance, there is some overhead in acquiring and releasing the monitor lock on the StringBuffer instance whenever any of its methods are invoked. Hence StringBuilder is a preferable choice in single thread environment.
StringBuilder is supposed to be a (tiny) bit faster because it isn't synchronised (thread safe).
You can notice the difference in really heavy applications.
http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html