In Java, a String, StringBuffer, and StringBuilder are all classes used to store and manipulate text data, but they differ in their functionality, immutability, and thread-safety.
String: A String is an immutable sequence of characters that cannot be changed once created. If you need to modify a String, a new String object must be created. Strings are thread-safe and can be shared among multiple threads.
StringBuffer: A StringBuffer is a mutable sequence of characters that can be modified after creation. StringBuffer is thread-safe, which means multiple threads can access it concurrently without any issue. StringBuffer is useful when there is a need for a mutable sequence of characters that can be modified efficiently without creating a new object every time.
StringBuilder: A StringBuilder is also a mutable sequence of characters, just like StringBuffer. However, StringBuilder is not thread-safe, which means it should not be used in a multi-threaded environment. StringBuilder is faster than StringBuffer, but if thread-safety is a concern, StringBuffer should be preferred over StringBuilder.
In general, String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
Storage area In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage area.
Mutability A String is immutable, whereas both the StringBuilder and StringBuffer are mutable.
Efficiency It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. The speed of a StringBuffer is more than a String and less than a StringBuilder. (For example appending a character is fastest in StringBuilder and very slow in String because a new memory is required for the new String with appended character.)
Thread-safe In the case of a threaded environment, StringBuilder and StringBuffer are used whereas a String is not used. However, StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads.
Syntax:
// String
String first = 'InterviewBit';
String second = new String('InterviewBit');
// StringBuffer
StringBuffer third = new StringBuffer('InterviewBit');
// StringBuilder
StringBuilder fourth = new StringBuilder('InterviewBit');
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Java, a String, StringBuffer, and StringBuilder are all classes used to store and manipulate text data, but they differ in their functionality, immutability, and thread-safety.
String: A String is an immutable sequence of characters that cannot be changed once created. If you need to modify a String, a new String object must be created. Strings are thread-safe and can be shared among multiple threads.
StringBuffer: A StringBuffer is a mutable sequence of characters that can be modified after creation. StringBuffer is thread-safe, which means multiple threads can access it concurrently without any issue. StringBuffer is useful when there is a need for a mutable sequence of characters that can be modified efficiently without creating a new object every time.
StringBuilder: A StringBuilder is also a mutable sequence of characters, just like StringBuffer. However, StringBuilder is not thread-safe, which means it should not be used in a multi-threaded environment. StringBuilder is faster than StringBuffer, but if thread-safety is a concern, StringBuffer should be preferred over StringBuilder.
In general, String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
String, StringBuffer, and StringBuilder can be differentiated as follows:
SYNTAX: