can you give difference between '+' and concat() in string .also compare with stringbuffer.append().
I understand:string concat() will take new memory but stringbuffer append() doesnt take new memory.
but i cannot understand. when to use which type of concatenation.
"You can concatenate multiple strings using either + operator or String.concat() or StringBuffer.append(). Which is the best one interms of performance?
The choice depends on two scenarios,first scenario is compile time resolution versus run time resolution and second scenario is wether you are using StringBuffer or String. In general, programmers think that StringBuffer.append() is better than + operator or String.concat() method. But this assumption is not true under certain conditions."
StringBuffer doesn't have the best performance - StringBuilder do
Back to your question...
It depend on how many + or String.concat() you need to use.
In the long run I'll say StringBuilder will out perform + or String.concat(), but if only you need a small number of them, then I'll not use the StringBuilder.
You can test it your self - code some very large loops where you append new Strings with +, concat(), StringBuilder and StringBuffer - and then put a start timer before each loop and a end timer after each loop.
If you want you can also monitor the memory use with the build in JConsole in Java.
String.concat() simply concats two strings. It takes two strings, and creates a third string with the result. This means that if you want to concat 100 strings together, you will have to do this operation 100 times, and create 100 new (and interim) strings in the process.
StringBuffer.append() will add the string to the end of the buffer. But it is not a string, and you will need to create the string with the toString() method call. This means that if you want to concat 100 strings together, you will have to call append() 100 times, followed by toString() -- creating only 1 string, in addition to the stringbuffer to concats the string. Very efficient in comparison to calling string concat() 100 times.
However, if you only want to concat two strings, using string buffer means that you will need to create the string buffer, append the strings, and then create the resultant string. This is obviously less efficient, and creates an extra object, when compared to calling string concat() once.
Using the "+" syntax is actually syntactic sugar. The compiler will replace the call with creating a string buffer/builder, appending the parts, and generating the resultant string.
Time taken for creation of StringBuffer: 14 milli seconds
Time taken for creation of StringBuilder: 8 milli seconds
Time taken for creation of String + objects: 4654 milli seconds
Time taken for creation of String.concat() objects: 9 milli seconds
Joined: Oct 13, 2005
That is a good example of how repeated catenation with + not in the one statement is very inefficient. Note you are using multiple +=s because you are calling that line many times.
When I said "in one statement", I wasn't clear. I should have said "one statement called once". Sorry about that. Because of the loop, that is equivalent to 30000 lines with a + in.
Rene, it looks like your code for concat() doesn't do what you think it does. Or at least, it does something very, very different from the other three code samples, and the times you quote are very misleading as a result.
Also, in Java performance tests, any test that runs in less than a second is probably too short to be meaningful. You need to greatly increase the number of iterations if you want to compare two techniques whose times are less than 100 millisecond. There are too many odd effects from JVM startup, and the variation from these effects can very easily dwarf the thing you're trying to measure.
Then again, you can just assume that any time that is in the tens of milliseconds is just really, really fast compared to methods that take more than a second. Don't bother comparing 8 ms to 14 ms - those differences are essentially meaningless here. But the difference between either of those and 4654 seconds is huge. That's the important part.
My own summary of when to use these techniques:
Use a StringBuilder anytime you need to build up a string in a loop, and the length of the string is expected to increase each time.
Use + everywhere else. Well, almost everywhere. But really, if you're not in a loop, no one will care about the performance benefit, so you might as well use the most readable alternative. Which, in my opinion, is using +.
Never use concat() - it's just a less readable version of +.
Never use StringBuffer, unless you're a time traveller trapped before 2005 or so. Use a StringBuilder instead. The reason for this is actually not performance - StringBuilder should be a little faster, sure, but who cares? The difference is minimal. The real reason is that some people think that StringBuffer is better for writing "thread-safe" code. Those people are almost always wrong, and you should avoid their advice in general. If you need thread safety, classes like StringBuffer are very poorly designed, and give people a false sense of security. Don't rely on them, and don't trust people who do.
Time taken for creation of StringBuffer with new String: 15 milli seconds
Time taken for creation of StringBuffer: 6 milli seconds
Time taken for creation of StringBuilder: 3 milli seconds
Time taken for creation of String + objects: 4651 milli seconds
Time taken for creation of String.concat() objects: 2316 milli seconds
Time taken for creation of StringBuffer with new String: 30 milli seconds
Time taken for creation of StringBuffer: 8 milli seconds
Time taken for creation of StringBuilder: 9 milli seconds
Time taken for creation of String + objects: 52594 milli seconds
Time taken for creation of String.concat() objects: 26316 milli seconds