String vs String Builder vs String Buffer

what is string?

String is a sequence of characters its may be either alphabets, numbers or symbols or combination of alphabets, numbers and symbols. In java string values are stored in String Constant Pool.

Java provide three classes for to represent the sequence of characters. those are

  • String
  • StringBuffer
  • StringBuilder

String:

The String is immutable class. Whenever you change the value of string JVMwill allocate new space in String Constant Pool. But if you give the same value which is already there in String Constant Pool Java will never create the new space for it, just create the reference of it. Java introduces the String Builder and String Buffer classes to make string object are mutable. But there is some difference between String Buffer and String Builder.

StringBuffer

  • Synchronized
  • Thread safe. Multiple threads can not access it simultaneously.
  • Less efficient than StringBuilder.

StringBuilder

  • Non Synchronized
  • Not Thread safe, so multiple threads can access simultaneously.
  • More efficient than StringBuffer.
string, string builder and string buffer