Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wat diff between stringbuffer & stringwriter

 
lax rao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The differences are superficial.
You will find many such duplications throughout the J2SE API Specification.
- An java.io.InputStream 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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. I should have looked at StringWriter closer.
 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringWriter uses StringBuffer internally

http://flexc.lcs.mit.edu/Harpoon/srcdoc/java/io/StringWriter.html
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So StringWriter is basically an Adapter that adapts a StringBuffer to the Writer interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic