• Post Reply Bookmark Topic Watch Topic
  • New Topic

String or StringBuffer?  RSS feed

 
Sri Addanki
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question on writing a method signature,
whether to pass an argument as a String or a StringBuffer?

The question is in the lines of the performance and also in the lines of
pass by value/reference.

Here's the scenario:

I'm writing a method to parse an xml and add the XML attributes to a file,
And within this file, I insert these values on particular linenumber/offset.

1. The file I get is in the form of a String
2. And in a HashMap I get the xml attributes, values
3. I call the method in a for loop while reading the xml values from the HashMap
4. So each time the method is called, the modified-file is sent as a String? or StringBuffer?
5. The size of the file will be max 32KB.

Can I pass the file as a StringBuffer Object rather than a String to the calling method. As this method is called in a loop several times, it is better to have the file as a String Buffer?

thanks, sri
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are editing a small (32K) xml document, why not parse it as a DOM tree and pass that around?
 
Sri Addanki
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
If you are editing a small (32K) xml document, why not parse it as a DOM tree and pass that around?


Hi Jeff, I'm not editing an xml document. I only edit an other text file( the size of which is 32kb) and to this I add the xml attributes.

The xml attributes are given to me in a array of HashMap[]. I don't have handle to the xml document.

thanks,sri
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general if you are constantly modifying/appending to a string value, particularly in a loop, you should use a string buffer.

What you describe sounds like a template/mail merge operation. - ie you start with some text, and then "fill in the blanks" - using a map of key/values passed in (the map being generated from xml)

I would probably go with passing and returning a string, but doing the manipulation with a stringbuffer.
ie the method would be something like

 
Sri Addanki
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan, thanks! I was considering the same too.

Because, once the original String is passed as StringBuffer, then it would be like pass by reference and the original value will be modified for good. Which I didn't want to happen.

I would like to modify the existing String and also have the original String. Thanks Any other inputs please...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sri,
You probably don't want to use a String or StringBuffer method parameter, but more likely, a CharSequence.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/CharSequence.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!