WHEN SHOULD WE USE StringBuffer AND StringBuilder ?

The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when
you have to make a lot of modifications to strings of characters. As we discussed in
the previous section, String objects are immutable, so if you choose to do a lot of
manipulations with String objects, you will end up with a lot of abandoned String
objects in the String pool. On the other hand,
objects of type StringBuffer and StringBuilder can be modified over and over again
without leaving behind a great effluence of discarded String objects.

A common use for StringBuffers and StringBuilders is file I/O when large,
ever-changing streams of input are being handled by the program. In these
cases, large blocks of characters are handled as units, and StringBuffer
objects are the ideal way to handle a block of data, pass it on, and then
reuse the same memory to handle the next block of data.

1 comment: