Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: difference between String Concatenation and String -Buffer/Builder .append(<value>)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default difference between String Concatenation and String -Buffer/Builder .append(<value>)

    in the book that im reading, the difference between concatenation and .append() doesnt explain the difference fully, only by some simple codes and some short statements implies a difference between the two

    given the following objects refers to a string value
    Concatenation:

    strObject = strObject + word;

     
    stringBufferObject.append(word);


    when i use an output statement(i.e System out), they dont differ too much, in fact they output in the same way, but one thing the book says that, with the use of StringBuffer or StringBuilder object, a program can avoid creating temporary String objects..

    its a bit complicated on my part. i need some low level english to understand this part

    what is the difference between a basic string concatenation and using .append(<string value>) of String Buffer/Builder object.
    Last edited by chronoz13; September 2nd, 2011 at 03:03 AM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: difference between String Concatenation and String -Buffer/Builder .append(<valu

    String is an immutable class. 'Immutable' means you can't change it, so if you need to transform a String object, you need to create at least one new String object. StringBuffer/Builder is mutable, so when you use it to concatenate strings, it merely keeps references to all the component strings and doesn't construct a new String object until you invoke its toString() method.

    '+' is pretty bad: it uses memory proportional to the *square* of the memory requirements of the component strings. Consider "www.javaprogrammingforums.com" built this way: "www" + "." + "javaprogrammingforums" + "." + "com" - you'd create these unused String objects too:
    "www."
    "www.javaprogrammingforums"
    "www.javaprogrammingforums."

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    chronoz13 (September 2nd, 2011)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: difference between String Concatenation and String -Buffer/Builder .append(<valu

    hmmm. so StringBuilder/Buffer(.append() method) is more preferable if you want to code "Carefully" your program when it comes to string processing(concatenating)? rather than just using "+"(Concat operator) explicitly? especially if your program deals with tons of String object/values processing?
    Last edited by chronoz13; September 2nd, 2011 at 07:07 AM.

  5. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: difference between String Concatenation and String -Buffer/Builder .append(<valu

    I use StringBuilder whenever I can purely because of the performance issue. I usually write long-lived Java applications for deployment on servers, so reducing the number of objects created for the same outcome is a VeryGoodThing. If you're writing a short-lived application it hardly matters at all, because chances are your JVM will exit before it needs to do garbage collection. '+' is nice and clear, easy to read, quick to type. I suspect there's every chance that if the popular java compilers don't already substitute StringBuilder for repeated '+', it can only be a matter of months before they do.

    If you find yourself constructing large objects that use a lot of String concatenation to deliver the end object to the user, you might notice a delay before the user gets her result if you're using '+'. If you suspect that a delay is down to '+', try using StringBuilder and compare your results. Optimising the performance of your code (and inevitably making it harder to read) when there's no detectable gain to the user isn't a good use of your time.

  6. The Following User Says Thank You to Sean4u For This Useful Post:

    chronoz13 (September 2nd, 2011)

  7. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: difference between String Concatenation and String -Buffer/Builder .append(<valu

    As sean already provided the basic answer why it is advantages to use apend rather then +. Just to add, out of StringBuffer & StringBuilder you should always try to use StringBuilder because it is much faster then StringBuffer coz is is not synchronized.
    Second thing is also try to use literals while dealing with String instead of creating String with "new" operator.
    You can check with this link

  8. The Following User Says Thank You to vivekanand30 For This Useful Post:

    chronoz13 (September 3rd, 2011)

  9. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: difference between String Concatenation and String -Buffer/Builder .append(<valu

    thanks for the link mate, i find it very useful, its a bit complicated for me dealing with references(memory addresses) and String pool, but the logic is clear on that article , thanks thanks greatly appreciated

Similar Threads

  1. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM