Since String is an immutable class every
time a new String is created and older one is discarded which creates lots of
temporary garbage in heap. If String are created using String literal they
remain in String pool.
To
resolve this problem Java provides us two Classes StringBuffer and StringBuilder. Both are mutable
means you can modify StringBuffer/StringBuilder object once you created it
without creating a new object.
String Buffer is an older class but
StringBuilder is relatively new and added in JDK 5. StringBuffer is very good
with mutable String but it has one disadvantage all its public methods are synchronized which
makes it thread-safe but
same time slow. In JDK 5 they provided similar class called StringBuilder in
Java which is a copy of StringBuffer but without synchronization
No comments:
Post a Comment