Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

When should I use a StringBuffer instead of a String?

String StringBuffer
0
10 Posted

When should I use a StringBuffer instead of a String?

0

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.

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.