Where to use a StringBuffer/StringBuilder than a String in Java?


  • The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.
  • A StringBuffer is synchronized while a StringBuilder is not synchronized.
  • A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.
  • If the Object value is not going to change use String Class because a String object is immutable.
  • If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
  • If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

Updated on: 30-Jul-2019

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements