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:
Code :
strObject = strObject + word;
Code :
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.
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."
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?
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.
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
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 :D greatly appreciated :D