-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?

Community
  • 1
  • 1
BoomirajP
  • 329
  • 2
  • 7
  • 14
  • 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? – Danail Nachev Aug 24 '11 at 08:59
  • 4
    Why don't you look it up yourself in the [API](http://download.oracle.com/javase/6/docs/api/)? – adarshr Aug 24 '11 at 09: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. – Danail Nachev Aug 24 '11 at 09:02
  • u can easily find this in java docs...??? are u interested in where we should use which??? – amod Aug 24 '11 at 09:05
  • yes.May i know where we should use which ? amod0017 – BoomirajP Aug 24 '11 at 09:16
  • @BoomirajP - there's lots been said about when to use which already on this very site: http://stackoverflow.com/search?q=[java]+string+stringbuffer – Flexo Aug 24 '11 at 09:26

5 Answers5

2

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

dacwe
  • 43,066
  • 12
  • 116
  • 140
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.

mckulpa
  • 374
  • 4
  • 9
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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
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"; 
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • 2
    In *almost all* cases a [`StringBuilder`](http://download.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) 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). – Joachim Sauer Aug 24 '11 at 09:01
  • 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! –  Aug 24 '11 at 09:04
  • 1
    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 Aug 24 '11 at 09:04
  • @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? > – Oh Chin Boon Aug 24 '11 at 09:16
  • 1
    It's somewhat implied in [§3.10.5](http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5): "String literals-or, more generally, strings that are the values of constant expressions ([§15.28](http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313))-are "interned" [...]." You can also easily verify it using `javap` by decompiling a simple test class. – Joachim Sauer Aug 24 '11 at 09:20
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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614