aspose file tools*
The moose likes Beginning Java and the fly likes String class: + vs concat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String class: + vs concat" Watch "String class: + vs concat" New topic
Author

String class: + vs concat

Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
Why is it recommended to use concat method instead of + operator? Please explain the difference. I'm a beginner.
regards,
sam.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Curious. I'd never loooked at concat before. About the only thing that looks different is when the string you are adding has zero length it just gives you back the original instead of creating a new String. The + operator can be a bit expensive ... if you are doing hundreds or thousands of string building operations look into StringBuffer.append(). It is common to see a method build up a StringBuffer and then return or use theBuffer.toString() at the end.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Where did you get the statement ? StringBuffers are recomended over String for concatination operations.

Originally posted by Sam Drake:
Why is it recommended to use concat method instead of + operator? Please explain the difference. I'm a beginner.
regards,
sam.


Groovy
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pradeep Bhat:
Where did you get the statement ? StringBuffers are recomended over String for concatination operations.

And even that's an oversimplification. StringBuffers are actually rather rarely needed to be used explicitely.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String class: + vs concat