Tuesday, October 23, 2007

String Vs. StringBuffer

String object is immutable, StringBuffer object is mutable.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated.

When a String is created, it is not destroyed. It remains in the pool of memory and whenever that string is referred, the JVM searches for the String from the string pool of memory and gets the relevant data. So as such String is expensive operation.
String buffer can be changed/modified based on our requirement. There are chances that you may need to form a query based on some conditions. Such queries can be formed using String buffer. Finally you can user the toString method to get the final String version of the stringBuffer.

No comments: