String vs StringBuffer vs StringBuilder

String:

  • A string is immutable.
  • Stored in the Constant String Pool.
  • Every immutable object in Java is ThreadSafe.
StringBuffer: 
  • StringBuffer is mutable, it can be changed after it is defined. 
  • Its object is stored in heap. 
  • It is also Thread Safe. 
  • Performance is slow because of thread safety. (Note: Thread Safe is nothing but no two threads can simultaneously access the same method).
StringBuilder
  • StringBuilder is same as StringBuffer. Value can be changed and the object is stored in heap. 
  • The only difference is that, it is not Thread Safe. 
  • It is better in terms of performance because it is not thread-safe and there are no checks done to verify thread safety.

No comments:

Post a Comment