This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic
Author

String and StringBuffer

Nimish Patel
Ranch Hand

Joined: Jun 29, 2005
Posts: 84
hi all,

we can concate string using string class and stringbuffer class.
but what is better from both ?

suppose when long string concate process ,
which one better , string or string buffer ?

please advise me.

Thanks,
Nimish Patel
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I'm going to move this to "Java in General (Beginner)" and then answer it.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

String catenation is implemented using StringBuffer. So, for example,

String s3 = s1 + s2;

will be compiled into something like

StringBuffer _tmp = new StringBuffer(s1);
_tmp.append(s2);
String s3 = _tmp.toString();

So really, they're the same. But if you're adding multiple Strings together, and you're doing it in a loop where performance matters, then you can generally write more efficient StringBuffer code by hand than the compiler will generate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String and StringBuffer