GeeCON Prague 2014*
The moose likes Performance and the fly likes String vs StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "String vs StringBuffer" Watch "String vs StringBuffer" New topic
Author

String vs StringBuffer

Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
Who is better/faster?


Wagner Danda<br />Sun Certified Programmer for Java 2 Platform
Stanley Tan
Ranch Hand

Joined: May 17, 2001
Posts: 243
That depends. If you are going to tack a lot of strings together, use the StringBuffer. But don't be fooled into the fact that
String s = "asdf" + "1234" + "asdf";
is slower than using a StringBuffer. It's after you initialize the string and you still want to append to it that you'll want to use a StringBuffer.
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
Originally posted by Stanley Tan:
That depends. If you are going to tack a lot of strings together, use the StringBuffer. But don't be fooled into the fact that
String s = "asdf" + "1234" + "asdf";
is slower than using a StringBuffer. It's after you initialize the string and you still want to append to it that you'll want to use a StringBuffer.

Is there a limit of size for StringBuffer objects? (not considering the computer´┐Żs memory )
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No - system memory is the only limit, unless there's a bug in your JVM (which I haven't heard of).


"I'm not back." - Bill Harding, Twister
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
Originally posted by Jim Yingst:
No - system memory is the only limit, unless there's a bug in your JVM (which I haven't heard of).

Thanks a lot!!!
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
Originally posted by Stanley Tan:
But don't be fooled into the fact that
String s = "asdf" + "1234" + "asdf";
is slower than using a StringBuffer.

well actually,
String s = "asd"+"er"+"df";
is faster than usign StringBuffer since the constant expression "asd"+"er"+df" is evaluated at compile time.
Is this what you meant?


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
 
GeeCON Prague 2014
 
subject: String vs StringBuffer