• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How StringBuffer provides synchronization?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain me with an example
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very simple: all of its methods are synchronized.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you rarely need synchronisation, so you usually use StringBuilder instead.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And you rarely need synchronisation, so you usually use StringBuilder instead.


Anyone else waiting for when they actually make StringBuffer a wrapper to a StringBuilder? Or indeed, when they create an interface that includes both? Maybe the first has already been done, but I've been waiting so long for the second, I've actually created my own (called StringHolder). Sheesh, software companies are dumb sometimes.

Winston
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would rather they just got rid of StringBuffer entirely. No need to keep the interfaces polluted with a useless class. If you need synchronization, you need it in a different place than what StringBuffer provides.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point, Mike S, but if you remove a class, then all the legacy code which depended on it is broken. It does say clearly in its documentation you ought to prefer StringBuilder, but so many people never seem to read that bit.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it would be a backward-incompatible change. Java is overdue for one of those, really - there's a lot of cruft that could be cleaned out, that Sun was never willing to ditch. I'm hoping Oracle will be more willing.

Having said that, like Winston I am surprised that they didn't retrofit a common interface onto StringBuilder/StringBuffer when Builder was introduced. Especially since it was Sun at the time. Oh well...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are lots of things they should have done, like creating a Stack interface …
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah - but most of those things, like Stack, are legacy things from Java 1.0 or 1.1. I'd grown used to Sun not touching that stuff for fear of breaking legacy code. But introducing StringBuilder came in 1.5, when Sun had much better design practices. Right then was the time to introduce a common interface, as they did for other API enhancements. Don't know why they didn't.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Yeah - but most of those things, like Stack, are legacy things from Java 1.0 or 1.1.


The fact is that what I suggested is not a legacy problem. It might, however, be a political problem.

@Java: Define a public interface in java.lang that implements ALL the methods of both classes (and there are LOTS of them; and they're already documented as being in sync), and put it in Version 8; and make both StringBuffer and StringBuilder implement it.

Most of us experienced bods know that that's what you should have done in the first place. So get on with it. If you don't want to apologise for all the problems that have been caused by having both a StringBuffer and StringBuilder class that do exactly the same thing, at least give us the chance to refactor our programs to use a standard interface.

Winston
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So... would this new interface be a sub-interface of CharSequence, which StringBuffer and StringBuilder both already implement, or would it be separate?
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subinterface. We want all those mutator methods as well; that's the point of having a StringBuffer/Builder. We might as well extend CharSequence since that's already part of the basic functionality.

The other issue is the various methods elsewhere that take a StringBuffer as argument. Like in java.util.regex.Matcher. Changing those signatures could create problems; they'd probably have to add overloads. Ugh. I think I'm back to wanting them to get rid of StringBuffer entirely, and force everyone else to deal with it.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Or indeed, when they create an interface that includes both?



Like this?

java.lang
Interface Appendable
Since: 1.5

Known Implementing Classes include StringBuffer, StringBuilder
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kind of. But we were imagining a much less crappy version of that.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I would rather they just got rid of StringBuffer entirely. No need to keep the interfaces polluted with a useless class.


There are a lot of other things in the standard Java API that would better be removed. For example the legacy collection classes such as Vector, Hashtable, Dictionary; Enumeration, the Date and Calendar classes (those should be replaced by something like Joda Time), the org.omg.* packages (for CORBA), etc. It's however not going to happen because it would break too many existing programs.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm dreaming of a day when all that stuff is removed. Or most of it, anyway. I think Oracle is more capable of envisioning a major change like this than Sun was. It may still be a long shot, but I think it's possible.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid going too much off topic, I started a new thread about that: Should old stuff be removed from a future Java version?
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic