How To Practice String Concatenation In Java?

Last updated on Jun 06,2023 4.9K Views

How To Practice String Concatenation In Java?

edureka.co

In this article, we tell you how String Concatenation In Java works and how it effects the strings if not properly concatenated. And also how we can efficiently concatenate Strings.

Following pointers will be covered in this article,

So let us get started with this article,

String Concatenation in Java

Concatenation means appending one or more Strings to others to create one big String. Suppose we want to concatenate first name and last name to create full name we can do so using concatenation.

There are different ways to concatenate String in Java, but the simplest one is by using ‘+’ operator. Even though Java doesn’t support operator overloading, this one is different in the case of String and + operator. + operator is used to add two numbers if both the operands are integers still in case of Strings we can use this to concatenate Strings. Let’s see an example to concatenate String

String fname = "Jon";
String lname = "Doe";
String newString = fname + " " + lname;
System.out.println(newString);

Output

Jon Doe

In such a way we can concatenate String using + operator. We can also concatenate String by using StringBuffer and StringBuilder, unlike String these classes are mutable and are used to modify String in place. But, in case of String class if we add two Strings we will always get a new String object.

If you keep adding String using + operator every time it adds two Strings it will create new object in heap, suppose if you concatenate String in a loop using + operator it may fill up the heap space causing memory issues. In order to solve this problem, two classes are provided in Java StringBuilder and StringBuffer which are mutable, so if we add two Strings it won’t create a new String object instead it will add new String to the same object. We will dive deep into this topic further in this article.

One more way to concatenate String is by using String.concat() method. Lets now look more deeply about how concatenation works using different ways.

Moving on with this article on String Concatenation In Java,

Different ways to Concatenate String

Let’s see how we can concatenate String by different methods:

Moving on with this article on String Concatenation In Java,

String concatenation using + Operator

The simplest way of doing concatenation is by using + operator.

Example

“hello”+“ ” +“edureka”+“!”

This example will create a new String as:

hello edureka!

You can add any number of string using this operator which will create a whole new String. we can also use String literal or String variable it will work in both the cases. The important thing to note here is that every time you add a String a new String gets created and none of the Strings get modified.

In the case of String literal if the String already exists in the pool it will give reference of that String but it will not create new String. Also, whenever you use + operator for concatenation make sure to give reference to a new String object. It is highly recommended not to use + operator for concatenation in loop else it will fill heap space with unnecessary String objects.

Moving on with this article on String Concatenation In Java,

Concatenate String using StringBuffer and StringBuilder class

The efficient way to join multiple String is by using StringBuffer and StringBuilder because these objects are mutable and take less time to build and append String. Using these classes if we are concatenating multiple Strings it will not create any new object because it updates the same String saving memory and saving garbage collection execution time. Also, if you want to make execution more efficient you can use StringBuilder class because this class is not synchronized. Following is the code using StringBuilder:

String result = new StringBuilder(20).append("Hello").append(" ").append("Edureka").toString();

Import point to remember is that we need to initialize StringBuilder with the required capacity i.e the number of characters in the result String which will save memory by efficiently allocating memory and save execution time.

Moving on with this article on String Concatenation In Java,

Program For Different ways of String Concatenation in Java

Below is a sample Java program which will show you how to use these three ways to concatenate String in Java. In the first example, we have used + operator, while in the second and third example we have used StringBuilder and StringBuffer class to join Strings in Java.

public class Concatenation
{
public static void main(String args[])
{
String stringOne = "Hello";
String stringTwo = "Edureka!";
//By using + operator
String finalString = stringOne + " " + stringOne;
System.out.println(finalString)
//by using StringBuilder
StringBuilder sb = new StringBuilder(14);
sb.append(stringOne).append(" ").append(stringTwo);
System.out.println(sb.toString())
//by using StringBuffer
StringBuffer sBuffer = new StringBuffer(15);
sBuffer.append(stringOne).append(" ").append(stringTwo);
System.out.println(sBuffer.toString());
}
}

Moving on with this article on String Concatenation In Java,

Performance of String concatenation in Java

As we have seen already we can easily add Strings using + operator in Java this method is fine if you want to concatenate fixed size Strings however if you are adding thousands of Strings using + operator it will affect the performance and memory utilization because it will create multiple Strings heap with no references as they are not mutable which will increase garbage collection execution time. Using StringBuffer or StringBuilder is better depending on the requirement you can use these classes as StringBuffer is thread-safe and StringBuilder is not.

Summary

In this article, we covered how to concatenate String using different methods. We concatenated Strings using different methods also which method is efficient to do the operation. Finally, a few points to remember are as follows:

While concatenation in the loop do not use + operator.

Discover the power of Flutter and learn how to leverage its features in a Flutter Development Course.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework