Swastik
Ashwini Surve wrote:what is the difference between string and stringbuffer in java??
Campbell Ritchie wrote:Welcome to the Ranch
![]()
You are obviously reading an old book; you ought not to use StringBuffer, but StringBuilder in most cases.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie wrote:So many people post as if they had only seen StringBuffer and never StringBuilder. There must be people reading JDK1.4 books still.
And the recommendation to use StringBuilder comes from its API documentation, and there is a similar recommendation in the StringBuilder docs.
Jeff Verdegan wrote:I can't imagine a real-world, uncontested situation where Buffer's syncing would cause a problem.
Jeff Verdegan wrote:
Campbell Ritchie wrote:
You are obviously reading an old book; you ought not to use StringBuffer, but StringBuilder in most cases.
I never got that. I know Builder is not synchronized, and therefore "faster", but uncontested lock acquisition has been fast since before Builder came into existence.
Jeff Verdegan wrote:I can't imagine a real-world, uncontested situation where Buffer's syncing would cause a problem. Maybe in JME or something?
luck, db
There are no new questions, but there may be new answers.
Henry Wong wrote:
Jeff Verdegan wrote:
Campbell Ritchie wrote:
You are obviously reading an old book; you ought not to use StringBuffer, but StringBuilder in most cases.
I never got that. I know Builder is not synchronized, and therefore "faster", but uncontested lock acquisition has been fast since before Builder came into existence.
I think they came around the same time. The huge performance gains regarding uncontested lock grabs appeared with Java 5 -- which also saw the appearance of the StringBuilder class.
Henry
Which brings us back to StringBuilderMike Simmons wrote: . . . if they'd gotten rid of the synchronization entirely.

Mike Simmons wrote:Yes, except that StringBuffer still exists, with synchronization, giving the impression that it serves a useful function somewhere. Which it does not.
Realistically, Sun would never just get rid of such a class, or remove the synchronization after it had been documented like that. But they could have at least deprecated StringBuffer when StringBuilder was introduced.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
| Consider Paul's rocket mass heater. |