When should I use a StringBuffer instead of a String?
In most cases you should use a StringBuffer rather than string concatenation. Java String objects are immutable; whenever you concatenate two String instances you actually create a third String object for the result. This implicit String object creation can slow down your program, increase the number of objects in the runtime system and the garbage collection required to dispose of the temporary strings. On a small scale, string concatenation is unlikely to have a significant performance impact, but if you are building strings in a for or while loop, or over many statement lines it is better to use a StringBuffer, or StringBuilder in single threaded applications.
In most cases you should use a StringBuffer rather than string concatenation. The character content of Java String objects is immutable. Whenever you concatenate two String instances you actually create a third String object for the result. This implicit String object creation can slow down your program, increase the number of objects in the runtime system and the garbage collection required to dispose of the temporary strings. On a small scale, string concatenation is unlikely to have a significant performance impact, but if you are building strings in a for or while loop, or over many statement lines it is better to use a StringBuffer, or StringBuilder in single threaded applications. Actions: Follow-up or correct this answer. Submit a new question.