Concatenate Strings Using StringBuilder.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 StringBuilder class is very similar to StringBuffer. It also has a method called append which can be used to concatenate Strings. The only difference is that StringBuffer is synchronised while StringBuilder is not. So StringBuilder has a slight performance advantage over StringBuffer.
The following code demonstrates the StringBuilder.append method.

So, this code is exactly the same as the StringBuffer code and it prints the following output:
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

Introduction to Apache Maven

1. What is Maven When you write a software application, there are many steps in it like adding the necessary JAR files, compiling the source...

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to...

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

Â

MORE IN THIS CATEGORY

Setting Max/Min Digits For Decimal Numbers With Java DecimalFormat

The DecimalFormat class offers the following four such methods which can be used to easily set the maximum and/or minimum digits for decimal numbers....

Remove Duplicates from List Using Set.addAll

The Set interface has a method called addAll. This accepts a 'Collection' as the parameter. So if you invoke this method by passing the...

Convert String to java.time.LocalDate with Formatter

The LocalDate class has an overloaded parse() method. In addition to the String Date, it accepts a DateTimeFormatter instance that specifies the format of...

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