Two Laptop Bag
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

Strings and StringBuffer


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

Joined: Jan 10, 2002
Posts: 63532


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.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11914

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

Joined: Oct 27, 2005
Posts: 20180

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.

How To Ask Questions How To Answer Questions
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
String >> StringBuffer > StringBuilder.


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


all events occur in real time
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3749


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


[OCA 8 Book] [Blog]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

This is a good link for this context.

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]
Don't get me started about those stupid light bulbs.
subject: Strings and StringBuffer
It's not a secret anymore!