Is StringBuffer deprecated?

In Effective Java, Josh Bloch says that

StringBuffer is pretty much outdated and the unsynchronized implementation of 'StringBuilder' should be replaced

.

But in my experience, I still saw the widespread use of the StringBuffer class. Why is the StringBuffer class deprecated and why is StringBuilder preferable to StringBuffer, with the exception of increased performance due to unsynchronization?

+45
java string stringbuilder stringbuffer
Jul 21 2018-11-11T00:
source share
5 answers

It's deprecated in that new Java 1.5 code should usually use StringBuilder - it's very rare that you really need to create strings in a thread-safe manner, so why pay the cost of synchronization?

I suspect that the code you see with StringBuffer basically ends up in buckets:

  • Written before Java 1.5
  • Written to be compatible with older JDKs
  • Written by people who do not know about StringBuilder
  • Autogenerated with tools that don't know about StringBuilder
+63
Jul 21 '11 at 11:08
source share

Not everyone reads as widely as you :-)

I'm only half-noise. People copy code and templates all the time. Many people do not communicate with API changes.

Why is StringBuffer deprecated? Because in the vast majority of cases, its synchronized behavior is not required. I can’t think of the time I have ever needed. Despite the fact that synchronization is not currently a performance issue, it once was, it makes no sense to pay this tax in scenarios where it is not needed.

+18
Jul 21 2018-11-11T00:
source share

Why is the StringBuffer class deprecated?

Because its operations are synchronized, which adds overhead and is rarely useful.

The reason you still find StringBuffer widely used is simply inertia: There are many more sample code examples that have never been updated to use StringBuilder , and people still learn obsolete practices (and not just that) from such sources. And even people who know better often return to old habits.

+9
Jul 21 '11 at 11:10
source share

I find it an outdated exaggeration.

StringBuffer is synchronized. StringBuilder - no.

In many (perhaps most) cases, you are not indifferent to the thread safety of what is used to create strings. You should use StringBuilder in these cases. In some cases, however, you may well want to make sure that the actions on the object are thread safe. StringBuffer is still useful in these cases.

+5
Jul 21 '11 at 17:06
source share

In most cases, synchronization is not only not required, but also gives readers a code of incorrect information, if you nevertheless use it: namely, the reader could assume that synchronization is required where it is not really.

Using StringBuilder instead advertises the fact that you do not expect access to cross-threads.

In fact, streaming data should always be done through well-defined communication channels in any case, and not just by accessing a synchronized string buffer. Therefore, I would recommend always using a different solution, even if a StringBuffer seems appropriate at first glance.

+4
Jul 21 2018-11-11T00:
source share



All Articles