-4

Possible Duplicate:
String, StringBuffer, and StringBuilder

What are common methods between String and StringBuffer? And what is the difference between string and string buffer?

6
  • 6
    Hm, there is plenty of resources out there, which describes the difference and you can easily see the common methods. So, why do you ask this question here? Commented Aug 24, 2011 at 8:59
  • 4
    Why don't you look it up yourself in the API? Commented Aug 24, 2011 at 9:00
  • And to quickly answer your question: String is immutable, while StringBuffer is mutable. When you have complex String, which you cannot construct in single step, you use StringBuffer which is converted to String. When you concatenate strings using '+', Java compiler converts this to calls to StringBuffer under the hood. Commented Aug 24, 2011 at 9:02
  • u can easily find this in java docs...??? are u interested in where we should use which??? Commented Aug 24, 2011 at 9:05
  • yes.May i know where we should use which ? amod0017 Commented Aug 24, 2011 at 9:16

5 Answers 5

2

The biggest difference is that String is immutable and StringBuffer is mutable.

Sign up to request clarification or add additional context in comments.

Comments

1

StringBuffer lets you join strings faster. For example the following code:

String s = "Initial string ";
for (int i = 0; i < 100; i++) {
    s = s + i;
}

on every iteration creates a string representing integer i and than also creates a new string of s and i joined together and moves s reference to this new object. It does not reuse the same s object by expanding its content.

StringBuffer lets you append string more efficiently. There is also a newer version of this class called StringBuilder which is basically StringBuffer without synchronization.

Comments

1

The first few lines on the StringBuffer JavaDoc give a pretty good answer:

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.

Comments

0

The most obvious shared methods are those specified by the interface implemented by both String and StringBuffer (as well as StringBuilder): Appendable. It provides 3 methods for appending other values.

Comments

0

When you need to concatenate many String literal together, then it might be better off for you to use a StringBuffer object, and as when you are done with the concatenation, turning it into a String object using the toString() method.

EDIT: Please ignore the lines below

The below instantiates 6 String objects in the String pool theoretically.

String a = "A" + "B" + "C" + "D" + "E"; 

5 Comments

In almost all cases a StringBuilder should be used over a StringBuffer: it provides the exact same API and only lacks synchronization (which is hardly ever useful on a StringBuffer anyway).
I reject the notion that StringBuilder should be used at all except in special situations like large template processing. If you have the "+"'s in your code, use what is most clear. Java Compilers may actually use StringBuilder for you!
Sorry to bother you again, but the example you gave does not produce 6 string objects. It produces a single String, because it's a constant expression.
@Joachim Sauer I always had the impression that 6 String objects are created, can you please reference me to any materials so that i may correct my understanding? ><
It's somewhat implied in §3.10.5: "String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" [...]." You can also easily verify it using javap by decompiling a simple test class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.