Java Notes
StringBuffer is a mutable, synchronized character sequence for text that must be changed safely by more than one thread.
StringBuffer is an object used to build or modify text. Unlike String, it is mutable: methods such as append, insert, and reverse change the same buffer. Its public operations are synchronized, so it is safer than StringBuilder when multiple threads genuinely share one buffer.
For ordinary single-threaded string building, prefer StringBuilder. Use StringBuffer only when shared mutable text needs its built-in synchronization.
String, StringBuffer, and StringBuilder
| Type | Can its characters change? | Thread-safe by design? | Normal use |
|---|---|---|---|
String | No; every modification creates a new string value. | Yes, because it is immutable. | Names, messages, constants, keys, small concatenations. |
StringBuilder | Yes. | No. | Repeated text construction inside one thread. |
StringBuffer | Yes. | Yes; its operations are synchronized. | A shared mutable buffer when synchronization is required. |
StringBuffer vs String — what changes in memory?
The important idea is not a raw memory address. It is object identity and mutability.
When message = message + " World" runs, the original String cannot be changed. Java produces a new String value and assigns message to that new object. In contrast, buffer.append(" World") changes the contents of the existing StringBuffer object.
Run this program and compare the two identity checks:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The output is:
originalMessage still refers to the original "Hello" string, so the comparison is false. Both buffer variables refer to the same mutable object, so the second comparison is true.
Build text with append
Use append when text is assembled in several steps. Each call returns the same buffer, so calls can be chained.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
report begins empty. The first append adds a label, the next adds the name, and so on. The newline characters create separate output lines. This is easier and more efficient than repeatedly assigning report = report + ... inside a large loop.
Insert, delete, and reverse
These methods modify positions in the same buffer.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
insert(5, "Hello ") puts characters before index 5. delete(5, 11) removes indexes 5 through 10; the end index is not included. reverse() reverses all characters in place.
Length and capacity
length() is the number of characters currently stored. capacity() is the amount of space available before the internal storage must grow.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The exact capacity is an implementation detail and is not normally business logic. If you know that you will build a very large buffer, you can provide an estimated capacity with new StringBuffer(expectedCharacters) to reduce resizing.
When StringBuffer is the right choice
Use StringBuffer only when all of these are true:
- Text changes repeatedly.
- More than one thread may access the same buffer.
- You need synchronization around individual buffer operations.
If only one thread constructs the text, use StringBuilder. If text should never change, use String.
What to remember
Stringis immutable; concatenation produces a new string value.StringBufferis mutable and synchronized.StringBuilderis usually the default for repeated changes in one thread.append,insert,delete, andreverseedit the buffer itself.delete(start, end)excludesend.
Continue the lesson path