Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String vs StringBuffer

 
Wagner Danda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who is better/faster?
 
Stanley Tan
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - system memory is the only limit, unless there's a bug in your JVM (which I haven't heard of).
 
Wagner Danda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic