• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings and StringBuffer

 
nivya
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI EVERYBODY
This is nivvi, i am new to this. I have doubt regarding strings and stringbuffers.can i use srtingbuffers instead of strings?if so, then which performance is better ?i mean working with strings is better one or stringbuffer is better one for increasing the performance of the progam.
 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You posted in the wrong forum. Please read the rules first. Use strings, unless you have reason to use stringbuffer.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"nivvi",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer to your question, like most software issues, is "it depends".

it depends on HOW you are using the objects, and WHAT you are doing with them. There are situation where one is perferable over the other, and vice versa.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As others have said already, String and StringBuffer are quite different, so there are not all that many situations where you could actually swap one for the other.

Note, though, that StringBuffer is mostly superceded by StringBuilder, in Java 5 onwards. You should not use StringBuffer in new code, but instead should use StringBuilder.

The reason for preferring StringBuilder is that it is not encumbered with pointless synchronisation. Very few uses of StringBuffer actually made use of its synchronisation, but all suffered the performance costs of it.

As a beginner, you don't really need to understand the paragraph above. Just remember that, if you are writing for Java 5 or newer, use StringBuilder.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to do a lot of string concatenation (string1 + string2), use StringBuilder instead of String. If you don't, it's up to you.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String >> StringBuffer > StringBuilder.

this

produced
String: 1328 ms
to compare it with the others it should be 20* as long.
so: String: 26560 ms
StringBuffer: 78 ms
StringBuilder: 62 ms



Yours,
Bu.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"nivya"

I'm afraid your name still does not conform to our naming policy. Please review the FAQ and update it appropriately.

Scott
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good link for this context.

http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm

Hope this will help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic