Difference Between String and StringBuffer
Think of a String as a printed page and a StringBuffer as a whiteboard. To change a printed page, you have to print a whole new page; the whiteboard you just edit. Both hold text, and both can produce the same final words, but they suit very different jobs. This question ("String vs StringBuffer") appears in almost every Java interview, so here's the full comparison with an example that shows each difference.
Side by Side in Code
public class Versus {
public static void main(String[] args) {
String s = "Hi";
s.concat(" Navika");
System.out.println(s);
StringBuffer sb = new StringBuffer("Hi");
sb.append(" Navika");
System.out.println(sb);
System.out.println("Hi".equals("Hi"));
StringBuffer x = new StringBuffer("Hi");
System.out.println(x.equals(new StringBuffer("Hi")));
}
}Hi
Hi Navika
true
falseHow it works
s.concat(" Navika")built a new string and threw it away;sis still"Hi". String is immutable.sb.append(" Navika")changed the buffer itself. StringBuffer is mutable.Stringoverridesequalsto compare text;StringBufferdoesn't, so two buffers with the same text aren'tequals.
The Full Comparison
String | StringBuffer | |
|---|---|---|
| Can the text change? | No (immutable) | Yes (mutable) |
| A "change" creates | A new object | Nothing new; the same object is edited |
| Thread safety | Safe, because nothing can change | Safe, because methods are synchronized |
equals | Compares text | Compares references (not overridden) |
hashCode | Based on text, cached | Identity-based |
| String pool | Literals are pooled | Never pooled |
Joining with + | Supported | Not supported; use append |
| Speed for many changes | Slow (copies every time) | Fast |
| Typical use | Values, keys, messages, anything stored or shared | Building text in many steps across threads |
| Since | Java 1.0 | Java 1.0 |
Common Mistakes
Storing a StringBuffer as a map key or in a set. Its equals and hashCode don't look at the text.
Returning a StringBuffer from a method that should return text. Return sb.toString(), so callers can't change it.
Using String with += in a big loop. That's the slow case; use a builder.
Interview Corner
What is the difference between String and StringBuffer?
String is immutable: its value can't change, every modification creates a new object, literals are pooled, and equals compares content. StringBuffer is mutable: methods such as append and insert modify the same object, it's synchronized and therefore thread-safe, it isn't pooled, and it doesn't override equals, so it compares references. Use String for values that are stored and shared, and a buffer for building text through many changes.
Practice
- Easy: Write the same 5-step text-building code with
Stringand withStringBuffer, printing the variable after each step. Where do they differ? - Harder: Put two
StringBufferobjects with the same text into aHashSetand print the set's size. Then put theirtoString()results in and compare.
Key Takeaways
Was this tutorial helpful?