aspose file tools*
The moose likes Beginning Java and the fly likes Strings and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings and StringBuffer" Watch "Strings and StringBuffer" New topic
Author

Strings and StringBuffer

nivya
Greenhorn

Joined: Sep 27, 2007
Posts: 1
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

Joined: Jul 07, 2007
Posts: 229
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

Joined: Jan 10, 2002
Posts: 61241
    
  66

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3707
    
    5

"nivya"

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

Scott


My Blog: Down Home Country Coding with Scott Selikoff
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

This is a good link for this context.

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

Hope this will help


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
Consider Paul's rocket mass heater.
 
subject: Strings and StringBuffer