• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is the best way to do string concatenation?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the best way to do string concatenation? I read that using "+" on a string object uses StringBuffer behind the scenes so it seems that using StringBuffer explicitly is unecessary.
So, should I do:
myString + "foo" + "bar";
or
myStringBuffer.append("foo").append("bar");
Any help is appreciated.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do the first you will end up with a String object that is immutable.
If you do the second you will end up with a StringBuffer object that is mutable.
Which do you WANT?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides what Cindy said another criteria for deciding whether to use a StringBuffer or String concatenation is the number of String Objects you need to concatenate. If it is only 2 both are equal in terms of performance . But if the number of Strings you want to concatenate is more, I will certainly prefer StringBuffer because everytime I concatenate two Strings a new Object is created( because String objects are immutable and cannot be modified) but if its a StringBuffer, the same Object is modified to hold the concatenated String and no new Objects are created.
Hope this helps !!
[ May 22, 2003: Message edited by: Arvind Dua ]
 
Marshal
Posts: 16594
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to consider readability before trying to second guess the compiler's optimization capabilities. A succession of append()s to a StringBuffer would probably throw another programmer off at first glance. Try to write clear code first; use a profiler to find bottlenecks.
[ May 22, 2003: Message edited by: Junilu Lacar ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic