File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes wat diff between stringbuffer & stringwriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "wat diff between stringbuffer & stringwriter" Watch "wat diff between stringbuffer & stringwriter" New topic

wat diff between stringbuffer & stringwriter

lax rao

Joined: Nov 30, 2005
Posts: 3
can any one explain difference between stringbuffer and stringwriter and when should we use them. can I pass stringwriter obj as argument to string.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
StringWriter is a Writer, it is nothing like StringBuffer and the purpose of each is so far from the other that it would be easier to explain the similarities which would be relegated to similarities that exist between all Objects. You should use a StringWriter when you want a Writer. You should use a StringBuffer when you need a mutable buffer for constructing Strings, or must construct a String in such a way that it cannot be done using String's constructors. In 1.5 you should use a StringBuilder instead.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The differences are superficial.
You will find many such duplications throughout the J2SE API Specification.
- An is just a java.util.Iterator<Byte>
- An array is just a broken attempt at the (still broken but less so) java.util.List.
- etc. etc.

Tony Morris
Java Q&A (FAQ, Trivia)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Indeed. I should have looked at StringWriter closer.
Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 312
StringWriter uses StringBuffer internally

Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
So StringWriter is basically an Adapter that adapts a StringBuffer to the Writer interface.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: wat diff between stringbuffer & stringwriter
It's not a secret anymore!