|
|
Stringbuffer And StringbuilderIn computer programming, the StringBuffer class is one of two core string classes in Java. Most of the time, the String class is used. The point of this class is it is a mutable object while String is immutable (see immutable object). That means the content of StringBuffer objects can be modifed while every methods of string class returns a new string and result in improving runtime efficiency. The StringBuilder, which is introduced by Java 1.5, differs from StringBuffer in that it is unsynchronized. StringBuffer and StringBuilder are included in the java.lang package. For example, following code uses StringBuffer instead of String for performance improvement. Reader reader = new FileReader("file"); char buf[] = new char10240; StringBuffer sbuf = new StringBuffer(0); while (reader.ready ()) { sbuf.append(buf, 0, reader.read(buf)); } Moreover, Java compiler (e.g., javac) usually uses StringBuffer to concatenate strings and objects joined by the additive operator. For example, one can expect: String s = "a + b = " + a + b; is translated to: StringBuffer sbuf = new StringBuffer (32); sbuf.append ("a + b = "); sbuf.append (a); sbuf.append (b); String s = sbuf.toString (); In the above code, a and b can be almost anything from primitive values to objects.
|
 |