Skip to main content

StringBuilder vs StringBuffer




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:

StringBuffer sb = new StringBuffer();
StringBuffer sb2;
int i, offset, len;
char c;
String s;
char chararr[];
Constructors

sb = new StringBuffer(); // Creates new, empty, StringBuffer
sb = new StringBuffer(n); // Creates new StringBuffer of size n
sb = new StringBuffer(s); // Creates new StringBuffer with initial value s
Using StringBuffer

sb2 = sb.append(x) //appends x (any primitive or object type) to end of sb.
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
Deleting from StringBuffer

sb2 = sb.delete(beg, end) //deletes chars at index beg thru end.
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.
Extracting Values from StringBuffer

c = sb.charAt(i) // char at position i.
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.
Searching in StringBuffer

i = sb.indexOf(s) //Returns position of first (leftmost) occurrence of s in sb.
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()
Converting values in StringBuffer

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) {
String result = s;
for (int i=1; i<times; i++) {
result = result + s;
}
return result;
}
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.

// More efficient version using StringBuffer.

public static String dupl(String s, int times) {
StringBuffer result = new StringBuffer(s);
for (int i=1; i<times; i++) {
result.append(s);
}
return result.toString();
}
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.

// Much more efficient version using StringBuffer.

public static String dupl(String s, int times) {
StringBuffer result = new StringBuffer(s.length() * times);
for (int i=0; i<times; i++) {
result.append(s);
}
return result.toString();
}
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.

Comments

  1. 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)

    But with everything, do some profiling before you make a code change for a theoretical performance improvement.

    ReplyDelete
  2. the java compiler even optimizes simple string concatenation to use stringbuilders in place.

    performance 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.

    ReplyDelete
  3. very nice post.Simple and explanatory.Good work!Thank you for sharing!! :)

    ReplyDelete
  4. good 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.
    good

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Health Center - Overview and Features

This video highlights the features available in Health Center. Health Center is a low-processor and memory usage diagnostic tool for monitoring the status of a running IBM Java Virtual Machine (JVM). Health Center provides live information and recommendations in each of the following areas: - Classes: information about classes being loaded - Environment: details of the configuration and system of the monitored application - Garbage Collection: information about the Java heap and pause times - Locking: information about contention on inflated locks - Profiling: sampling profile of Java methods including call paths

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Java Garbage Collection

Explain Garbage collection mechanism in Java? Garbage collection is one of the most important features of Java. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. In Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic pro...