String vs StringBuilder vs StringBuffer
date
Sep 22, 2024
slug
string-stringbuilder-stringbuffer
status
Published
tags
Java
Concept
String
summary
type
Post
Java provides three classes to represent a sequence of characters:
String, StringBuffer, and StringBuilder.String is one of the most widely used classes in Java. StringBuffer and StringBuilder classes provide methods to manipulate strings.String
String is immutable in Java. So it’s suitable to use in a multi-threaded environment. We can share it across functions because there is no concern of data inconsistency.We can instantiate
String in two ways:When we create a String using double quotes,
JVM first looks for the String with the same value in the string pool. If found, it returns the reference of the string object from the pool. Otherwise, it creates the String object in the String pool and returns the reference. JVM saves a lot of memory by using the same String in different threads.If the
new operator is used to create a string, it gets created in the heap memory.String concatenation operator (+) internally uses
StringBuffer or StringBuilder class.String vs StringBuffer
Since
String is immutable in Java, whenever we do String manipulation like concatenation, substring, etc. it generates a new String and discards the older String for garbage collection. These are heavy operations and generate a lot of garbage in heap.So Java has provided
StringBuffer and StringBuilder classes that should be used for String manipulation. StringBuffer and StringBuilder are mutable objects in Java.StringBuffer vs StringBuilder
StringBuffer was introduced in Java 1.0 whereas StringBuilder class was introduced in Java 1.5 after looking at shortcomings of StringBuffer.StringBuffer provides Thread safety (all of its public methods are synchronized) but at a performance cost. In most of the scenarios, we don’t use String in a multithreaded environment. So Java 1.5 introduced a new class StringBuilder, which is similar to StringBuffer except for thread-safety and synchronization.StringBuilder performs better than StringBuffer, and is better suited in most of the general programming scenarios. If you are in a single-threaded environment or don’t care about thread safety, you should use StringBuilder. Otherwise, use StringBuffer for thread-safe operations.StringBuffer has some extra methods such as substring, length, capacity, trimToSize, etc. However, these are not required since you have all these present in String too. That’s why these methods were never implemented in the StringBuilder class.StringBuffer | StringBuilder |
Synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. | Non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously. |
Suitable for multi-threaded environments | Suitable for single-threaded environments |
Slightly less efficient due to thread-safety | More efficient |
Slightly more memory allocation overhead | Less memory allocation overhead |
StringBuffer was introduced in Java 1.0 | StringBuilder was introduced in Java 1.5 |
Mutable | Mutable |
Recommended in concurrent scenarios, thread-safe environments | Recommended in non-concurrent scenarios, performance-critical operations |


