StringBuffer is used to store character strings that will be changed (String objects cannot be changed). It automatically expands (buffer size) as needed. Related classes: String, CharSequence.
StringBuilder was added in Java 5.0. It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster. No imports are necessary because these are both in the java.lang package.
StringBuffer and StringBuilder methods and constuctors
Assume the following code:
An interesting aspect of the append() and insert() methods is that the parameter may be of any type. These methods are overloaded and will perform the default conversion for all primitive types and will call the toString() method for all objects.
Chaining calls in StringBuffer
Some StringBuffer methods return a StringBuffer value (eg, append(), insert(), ...). In fact, they return the same StringBuffer that was used in the call. This allows chaining of calls. Eg,
Because a StringBuffer object is mutable (it can be changed), there is no need to allocate a new object when modifications are desired. For example, consider a method which duplicates strings the requested number of times.
// Inefficient version using String.
// More efficient version using StringBuffer.
// Much more efficient version using StringBuffer.
StringBuilder was added in Java 5.0. It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster. No imports are necessary because these are both in the java.lang package.
StringBuffer and StringBuilder methods and constuctors
Assume the following code:
StringBuffer sb = new StringBuffer();Constructors
StringBuffer sb2;
int i, offset, len;
char c;
String s;
char chararr[];
sb = new StringBuffer(); // Creates new, empty, StringBufferUsing StringBuffer
sb = new StringBuffer(n); // Creates new StringBuffer of size n
sb = new StringBuffer(s); // Creates new StringBuffer with initial value s
sb2 = sb.append(x) //appends x (any primitive or object type) to end of sb.Deleting from StringBuffer
sb2 = sb.append(chararr, offset, len) //appends len chars from chararr starting at index offset.
sb2 = sb.insert(offset, x) // inserts x (char, int, String, ...) at position offset.
sb.setCharAt(index, c) // replaces char at index with c
sb2 = sb.delete(beg, end) //deletes chars at index beg thru end.Extracting Values from StringBuffer
sb.setLength(n) // Sets the length of the content to n by either truncating current content or extending it with the null character ('\u0000').
Use sb.setLength(0); to clear a string buffer.
c = sb.charAt(i) // char at position i.Searching in StringBuffer
s = sb.substring(start) // substring from position start to end of string.
s = sb.substring(start, end) // substring from position start to the char before end.
s = sb.toString() // Returns String.
i = sb.indexOf(s) //Returns position of first (leftmost) occurrence of s in sb.Converting values in StringBuffer
i = sb.lastIndexOf(s) // Returns position of last (rightmost) occurrence of s in sb.
Misc
i = sb.length() // length of the string s.
sb2 = sb.reverse()
An interesting aspect of the append() and insert() methods is that the parameter may be of any type. These methods are overloaded and will perform the default conversion for all primitive types and will call the toString() method for all objects.
Chaining calls in StringBuffer
Some StringBuffer methods return a StringBuffer value (eg, append(), insert(), ...). In fact, they return the same StringBuffer that was used in the call. This allows chaining of calls. Eg,
sb.append("x = ").append(x).append(", y = ").append(y);Efficiency of StringBuffer compared to String
Because a StringBuffer object is mutable (it can be changed), there is no need to allocate a new object when modifications are desired. For example, consider a method which duplicates strings the requested number of times.
// Inefficient version using String.
public static String dupl(String s, int times) {If called to duplicate a string 100 times, it would build 99 new String objects, 98 of which it would immediately throw away! Creating new objects is not efficient. A better solution is to use StringBuffer.
String result = s;
for (int i=1; i<times; i++) {
result = result + s;
}
return result;
}
// More efficient version using StringBuffer.
public static String dupl(String s, int times) {This creates only two new objects, the StringBuffer and the final String that is returned. StringBuffer will automatically expand as needed. These expansions are costly however, so it would be better to create the StringBuffer the correct size from the start.
StringBuffer result = new StringBuffer(s);
for (int i=1; i<times; i++) {
result.append(s);
}
return result.toString();
}
// Much more efficient version using StringBuffer.
public static String dupl(String s, int times) {Because StringBuffer is created with the correct capacity, it will never have to expand. There is no constructor which allows both an initial capacity to be specifed and an initial string value. Therefore the loop has one extra iteration in it to give the correct number of repetitions.
StringBuffer result = new StringBuffer(s.length() * times);
for (int i=0; i<times; i++) {
result.append(s);
}
return result.toString();
}
The Hotspot VM can do Escape Analysis to detect single-threaded use of StringBuffer and avoid the synchronization. (Obviating the need, perhaps, to use StringBuilder at all)
ReplyDeleteBut with everything, do some profiling before you make a code change for a theoretical performance improvement.
the java compiler even optimizes simple string concatenation to use stringbuilders in place.
ReplyDeleteperformance is not as much an issue as you might think. there is much more trouble with memory if you keep creating new strings in a loop!
strings are immutable and they will probably stick around in your app and won't get freed by the garbage collector.
very nice post.Simple and explanatory.Good work!Thank you for sharing!! :)
ReplyDeletegood post you have indeed covered the topic with great details. I have also blogged my experience as String vs StringBuffer vs StringBuilder let me know how do you find it.
ReplyDeletegood