DEV Community

DHANRAJ S
DHANRAJ S

Posted on

String vs StringBuffer vs StringBuilder in Java — My Notes

Today I am reading about StringBuffer and StringBuilder in Java. I always used String for everything — but my trainer told me there are situations where String is not the right choice. So I went and read about it, and writing my understanding here.


String (Oracle Docs)

"Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared."

StringBuffer (Oracle Docs)

"A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls."

StringBuilder (Oracle Docs)

"A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread."


What is String?

String is a class in Java used to store a sequence of characters.

The most important thing about String is immutable. That means once a String is created, it cannot be changed.

String name = "Ravi";
name = name + " Kumar";
Enter fullscreen mode Exit fullscreen mode

Every time I do any operation on a String, a new object is created in memory. The old one is left behind.


What is StringBuffer?

StringBuffer is a class in Java that works like String but it is mutable. That means I can change the content of the same object without creating a new one.

StringBuffer sb = new StringBuffer("Ravi");
sb.append(" Kumar");
System.out.println(sb);  // Ravi Kumar
Enter fullscreen mode Exit fullscreen mode

No new object was created here. The same sb object was modified directly.

StringBuffer is also thread-safe which means it can be safely used when multiple threads are running at the same time.


What is StringBuilder?

StringBuilder is almost the same as StringBuffer. It is also mutable same object gets modified, no new object.

StringBuilder sb = new StringBuilder("Ravi");
sb.append(" Kumar");
System.out.println(sb);  // Ravi Kumar
Enter fullscreen mode Exit fullscreen mode

The only difference from StringBuffer StringBuilder is not thread-safe. But because of that, it is faster than StringBuffer.


Why do StringBuffer and StringBuilder exist?

The reason I understood is String creates a new object every time it is modified.

If I am doing a lot of string operations in a loop, String becomes very slow and wastes memory.

Here, 5 new String objects are created in memory. For 5 it is okay. But if the loop runs 10,000 times, that is 10,000 objects. The program slows down and uses a lot of memory.

With StringBuilder:

Same output. But only one object throughout. Much more efficient.


Common Methods

All three — String, StringBuffer, StringBuilder share some methods. But StringBuffer and StringBuilder have extra ones for modifying content.

I tried all of these and they all work on the same object. No new object gets created at any step.


Difference Table — String vs StringBuffer vs StringBuilder


References

Top comments (1)

Collapse
 
buddingdeveloper profile image
Ebenezer

Nice blog dhanraj! ,You said the difference between stringbuilder and stringbuffer is no thread safe Can you explain what is thread safe ,what role it plays in these two?