What is the use of StringBuilder class in java?

Modified on Tue, 28 Mar, 2023 at 3:55 PM

In Java, StringBuilder is a class that provides a more efficient way to build strings compared to using the + operator or String concatenation. This is because the + operator and String concatenation create a new string object every time a concatenation is performed, which can be inefficient for building large strings. StringBuilder avoids this problem by providing a mutable buffer for constructing strings.


Here's an example of using StringBuilder in Java:

In this example, we create a new StringBuilder object and append two strings to it using the append() method. Then, we convert the StringBuilder object to a String using the toString() method, and finally, we print the resulting string to the console.

The output of this program will be:

This example demonstrates how to build a string using StringBuilder by appending strings together, which is more efficient than using the + operator or String concatenation. Additionally, StringBuilder provides other methods for manipulating the buffer, such as insert(), delete(), and replace(), which can be useful for more complex string manipulation.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article