• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings and StringBuffer

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic