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 String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
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.
 
Consider Paul's rocket mass heater.
 
subject: String and StringBuffer
 
Similar Threads
Alternative to StringBuffer
why dont we prefer to use StringBuffer
Why StringBuffer not override equals??
url reading problem
About String