File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Difference between '+' and  concat in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between Watch "Difference between New topic
Author

Difference between '+' and concat in string

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
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."
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Just a comment...

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.


Regards, Rene Larsen
Dropbox Invite
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . at present. In Java8 there will be a much faster way to do that, and the compiler will alter the interpretation of the + sign.

Or maybe it won't. But that is why it is actually more efficient to use the + operator than explicitly to call StringBuilder.
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Does String constant pool occupies memory or not
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Yes. The String constants need to be stored somewhere, and that somewhere is in memory.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I have checked this code . but still am not clear. can you explain

"+" operator is best in which situation
String. concat is best in which situation
StringBuffer.append() is best in which situation
StringBuilder.append() is best in which situation.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
It is best to use + and to keep all your +s in one statement. That is because that is what the optimisation in the compiler is designed to handle.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179


this run gives the following output:

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Yeah, you are right - to mush copy/paste


here are the new times

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
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

and with a loop of 100000, it gives this result

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between '+' and concat in string
 
Similar Threads
Concatenating Strings
string convert?
Which one of these two programs execute fast
StringBuffer
illegal character: \92