This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between concat() method and '+'operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between concat() method and Watch "Difference between concat() method and New topic
Author

Difference between concat() method and '+'operator

Vijaylaxmi Agarwal
Greenhorn

Joined: Sep 23, 2008
Posts: 3
Hi all,


Can any tell me
what is the difference between concat() method and '+'operator in String
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

I think in functional terms that there is no difference between '+' and .concat(), but there is a difference in performance for larger scale applications.

It has to do with way that strings are stored in Java, they are immutable objects and in concatenating them a new object is created from both (I think)

So the '+' is more of a convenience operator that does not scale well, and cuts performance in loops etc, but this shouldn't effect small projects.

See Joshua Bloch, 2nd Ed, Item 51. Beware the performance of string concatenation.
[ September 24, 2008: Message edited by: marten kay ]

when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

From the JDK 1.6 source code:


The + operator on the other hand:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

Java API for java.lang.String


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

So the '+' is more of a convenience operator that does not scale well, and cuts performance in loops etc, but this shouldn't effect small projects.


There are actually a couple of cases where the "+" operator is more efficient than the string concat() method...

For compile time constants, "+" will be done during compile time. And the result is actually stored as a literal. This is actually much more efficient than doing a concat() at runtime.

For long chained operations -- such as strA + strB + strC + strD -- the "+" operator is more efficient. This is because a single stringbuilder is created, and all the strings are appended, generating only a single builder and one string for all the "+" operations in the expression.


Actually, now that I think of it, this second case is probably more common than just the concat of two strings.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prateek Kumar Singh
Greenhorn

Joined: Jan 21, 2011
Posts: 29

Hi

Case 1. When we are using + operator then like StringBuilder is used to concatenate. After concatenation the resultant StringBuffer or StringBuilder is changed to String.Its take following steps

1. A StringBuffer object is created
2. string1 is copied to the newly created StringBuffer object
3. The “*” is appended to the StringBuffer (concatenation)
4. The result is converted to back to a String object.
5. The string1 reference is made to point at that new String.
6. The old String that string1 previously referenced is then made null.

Case 2. In case of string concatenation
str1.concat(str2) simply makes a new string that's the concatenation of str1 and str2, which is less expensive for a small number of concatenated strings .



cheers
Prateek Singh


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

prateek garg wrote:Hi

Case 1. When we are using + operator then like StringBuilder is used to concatenate. After concatenation the resultant StringBuffer or StringBuilder is changed to String.Its take following steps

1. A StringBuffer object is created
2. string1 is copied to the newly created StringBuffer object
3. The “*” is appended to the StringBuffer (concatenation)
4. The result is converted to back to a String object.
5. The string1 reference is made to point at that new String.
6. The old String that string1 previously referenced is then made null.

Case 2. In case of string concatenation
str1.concat(str2) simply makes a new string that's the concatenation of str1 and str2, which is less expensive for a small number of concatenated strings .



However, concat() simply does effedtively the same char array manipulation as concat() does. The only real difference is the creation of the SB object. You'd be very unlikely to ever notice a performance difference between using concat() and +=.


Also, please don't revive long-dead therads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between concat() method and '+'operator
 
Similar Threads
Difference between '+' and concat() method in String
String question
String class: + vs concat
String Concatination
append AND concat