Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between concat() method and '+'operator

 
Vijaylaxmi Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


Can any tell me
what is the difference between concat() method and '+'operator in String
 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prateek Kumar Singh
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic