Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String and StringBuffer

 
Nimish Patel
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to "Java in General (Beginner)" and then answer it.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic