StringBuilder over StringBuffer
As String is an immutable class , any update operations like replace/substring/append is very expensive. As solution for this StringBuffer class has been introduced for all string update related operations.
But problem with StringBuffer is that it is a synchronized class. All its public methods are tagged as “synchronized”. This adds an extra cost to string update operation whenever synchronized is actually not needed.
As part of JDK 1.5 a new utility class called StringBuilder has been introduced for non-synchronized string update operations. StringBuilder is designed as a replacement for StringBuffer in single-threaded usage.
It means using StrinBuilder instead of StringBuffer where ever synchronization is not an issue will be added to program performance.
Comments
Post a Comment