Concatenate Strings Using StringBuffer.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these classes is that both these classes are mutable, so using them does not result in the creation of new String objects. So StringBuffer and StringBuilder have a better performance as compared to the + operator and concat method.

The StringBuffer class has a method called append which can be used to concatenate Strings. It appends the value passed in with the StringBuffer on which it is invoked. The following code demonstrates this.

As before, this code creates string1 and string2. It also creates a StringBuffer called result. It then invokes the append method with both the Strings. So, this code also prints the same output as before: Hello World

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

RELATED POSTS

Java DecimalFormat Class

When you need to format decimal numbers, such as taking three or two decimal places for a number, showing only the integer part of...

Convert List to Array Using Stream without Param

Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() has been added...

Convert List to Array Using ToArray Without Param

Java provides a toArray method on the 'List' interface. This can be used to convert a 'List' to an array. Since there are are...

Â

MORE IN THIS CATEGORY

How To Setup Automatic SSH-Agent for GitBash

Using below procedure, you shall be able to setup SSH-Agent to run automatically whenever GitBash is launched on Windows. The ssh-agent process will continue...

Convert List to Array Using Stream without Param

Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() has been added...

Format Decimal Numbers with Grouping Separator

The DecimalFormat class has a method called setGroupingSize() which sets how many digits of the integer part to group. Groups are separated by the...

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

SIMILAR ON CODEX

- Advertisement -spot_img