-1

I was just going through the net but I could not find a clear & precise

difference between String ,StringBuilder & StringBuffer .

Please also explain when should we use them and what do we mean by thread-safe in java ?

Prateek Joshi
  • 3,383
  • 3
  • 34
  • 45
  • possible duplicate of [What is the difference between String and StringBuffer in Java?](http://stackoverflow.com/questions/2439243/what-is-the-difference-between-string-and-stringbuffer-in-java) – Vicky Thakor Jun 18 '15 at 10:28
  • But I also want the different cases in which they can be used (which is not mentioned on that post). – Prateek Joshi Jun 18 '15 at 10:30
  • 1
    possible duplicate of [String, StringBuffer, and StringBuilder](http://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder) – Paolo Jun 18 '15 at 10:33
  • Use StringBuilder when you want to _build_ a _String_, and see Tagir Valeev's answer for why you don't want to use StringBuffer ever. StringBuffer is obsolete. – Solomon Slow Jun 18 '15 at 19:14

2 Answers2

1

I will clarify the thread-safety point. The other points are well described in older questions.

It's quite rare case when StringBuffer suits your needs. While it's thread-safe it doesn't mean you will get what expected when using it from different threads. For example, suppose you have the following code:

StringBuffer buf = new StringBuffer();

public void appendMessage(String message) {
    buf.append("INFO: ").append(message).append(System.lineSeparator());
}

If you are using it in multithread environment it will not fail, but you may end up having content like this:

INFO: INFO: thread1 message
thread2 message

That's because individual append calls are synchronized, but the whole sequence is not.

In order to ensure that your messages are separately added, you must have an external synchronization like this:

Object lock = new Object();
StringBuilder buf = new StringBuilder();

public void appendMessage(String message) {
    synchronized(lock) {
        buf.append("INFO: ").append(message).append(System.lineSeparator());
    }
}

Here the whole sequence of calls is synchronized, so you will have the whole message appended at once. And as you are using the external synchronization, StringBuilder will work fine as well.

So in general StringBuffer should not be used in the most of situations.

Tagir Valeev
  • 87,515
  • 18
  • 194
  • 305
1

Main difference between String and StringBuilder/StringBuffer is that String is immutable, i.e. a String you once have initialized cannot be changed anymore. Redefining a String just creates a new immutable String in memory. Instances of StringBuilder/StringBuffer can be modified by methods like append(String str) or insert(StringBuffer sb). The only difference between StringBuilder/StringBuffer is that StringBuilder is not safe for use by multiple threads, StringBuffer instead is thread-safe.

So if you want to use string objects that you define once and do not need to modify a lot, you use String. If you want to work on them you use StringBuilder, and if you want to do this in a multithreaded environment you use StringBuffer.

RamsesGE
  • 33
  • 8