String

StringBuffer

StringBuilder

Modification

immutable

Mutable

Mutable

Storage Area

Constant String Pool

Heap

Heap

Thread safe

 

Synchronized i.e. thread safe

Non-Synchronized i.e. not thread safe

Performance

Fast

slow

Fast

Speed on Concat too many strings

Slow and consumes more memory

 

Fast and consume less memory

 

Overrides equals() method of Object class

Not such

Not such

 

  • Simply use StringBuilder unless you really are trying to share a buffer between threads. StringBuilder is the unsynchronized (less overhead = more efficient) younger brother of the original synchronized StringBuffer class.
  • Thread safe /Synchronized: Methods of Stringbuffer cannot be called by two or more threads simultaneously
  • If content is fixed and is not changed frequently    - > STRING
  • If content is not fixed and changes frequently :
    1. If thread safety required -> StringBuffer
    2. If thread safety not required -> String Builder

Post a Comment

Previous Post Next Post