Java String Concatenation Examples [4 Methods]


JAVA

Author: Bashir Alam
Reviewer: Deepak Prasad

 

Introduction to Java String concatenation

Concatenation is the process of appending one string to the end of another string. So java string concatenation is the process of adding or appending multi strings together. In this tutorial, we will learn about java string concatenation. We will cover multiple ways to concatenate strings in including the plus operator, String.concat() method, StringBuilder class, and StringBuffer class.

We will also take various examples as we will go through each of the methods. All in all, this tutorial will contain all the necessary and useful information that you need to know in order to start working with strings and concatenating them.

 

Getting started with Java string concatenation

A Java String is a sequence of characters that exist as an object of the class java. They are mutable which means once they are created they cannot be changed but they can be appended together by using java concatenation methods. You need to have some basic knowledge of strings in order to understand the concatenation. You can learn about strings from the article Java strings. In this tutorial, we will cover multiple ways to concatenate the strings using a java programming language.

 

Method-1: Java string concatenation using plus operator

The simplest and easiest way to of strings concatenation in java is using the plus operator.  Using the + operator, we can combine more than one string. One important thing which is to be noted is that the String obtained after concatenation using + operator will be a new String pointing to the fresh memory location in the java heap. If a string object exists in the pool, then it returns the same string object, otherwise, it creates a new object.

Here is the simple syntax of string concatenation in java using the plus operator.

string1 + string2

This will return a new string that contains string1 and string2 together.

 

Example-1 Concatenate two strings using the plus operator

Now we are familiar with the basic syntax of java string concatenation using the plus operator. Let us now take an example and concatenate the two strings. See the example below:

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "Welcome to ";
		String string2 = "Golinuxcloud";
		// java string concatenation using +
		String string3= string1 + string2;
		// printing the new string
		System.out.println(string3);
	}
}

Output:

Welcome to Golinuxcloud

Notice that we get the appended string of string1 and string2. We can also append the strings within the printing method instead of creating a new variable. See the example below, where we directly printed the appended strings without explicitly creating a new string variable.

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "Welcome to ";
		String string2 = "Golinuxcloud";
		// printing the new string
		System.out.println(string1+string2);
	}
}

Output:

Welcome to Golinuxcloud

 

Example-2 Concatenation of multiple strings using the plus operator

In the above, examples we had taken only two strings and we concatenate them using the plus operator. In this section, we will take more than two strings and will concatenate them together using the plus operator. See the example below:

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "Welcome ";
		String string2 = "to ";
		String string3= "Go";
		String string4= "linux";
		String string5= "cloud";
		// printing the new string by appending multiple strings
		System.out.println(string1+string2+string3+string4+string5);
	}
}

Output:

Welcome to Golinuxcloud

Notice that we can add as many strings as we want using the plus operator, it will return a single string containing all the appended strings.

 

Example-3 Java concatenate the string with other data types using the plus operator

The plus operator can also be used to add integers and floating points to the String as well. In this section, we will see how we can append other data type values to the strings. See the example below, which appends floating-point and integers to the string value.

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "The total courses are : ";
		String string2 = "My height is : ";
		// concatenating integers and floating points
		String string3 = string1 + 8;
		String string4 = string2 + 174.3; 
		// printing the new string 
		System.out.println(string3);
		System.out.println(string4);

	}
}

Output:

The total courses are : 8
My height is : 174.3

Notice that we successfully added integer and floating values to the String using the plus operator.

 

Method-2: Java string concatenation using String.concat() method

The java string concat() method concatenates one string to the end of another string. This method returns a string with the value of the string passed in to the method, appended to the end of the string. The following is the simple syntax of the java String.concat() method.

String1Name.concat(String2Name);

Notice that the concrete method takes a string as an argument and append or adds it to the specified string in the beginning.

 

Example-1 Concatenation of two strings using String.concat() method

Now, we know the basic syntax of java string.concate method. Let us take an example and see how we can add two strings using this method. See the example below:

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "Welcome to  ";
		String string2 = "golinuxcloud";
		// java string concatenation using string.concat() method
		String string3 = string1.concat(string2);
		// printing the new string 
		System.out.println(string3);
	}
}

Output:

Welcome to golinuxcloud

Instead of creating a new string variable for the concatenation, we can also print the concatenated string directly. See the example below:

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "Welcome to  ";
		String string2 = "golinuxcloud";
		// printing the new string 
		System.out.println(string1.concat(string2));
	}
}

Output:

Welcome to golinuxcloud

Notice that we did not create a new variable to store the appended string, in fact, we directly print it. The disadvantage of String.concate method is we cannot concatenate more than two strings at the same time because the string Concat method only takes one argument. Also, we cannot add integers or floating points to the string using this method. If we try to add, we will get an error. See the example below:

// Java main class
public class Main{
	// java main method
	public static void main(String[] args) {
		// creating string variables
		String string1 = "The total courses are : ";
		Integer string2 = 5;
		// java string concatenation 
		String strin3 = string1.concat(string2);
	}
}

If we run the above code, we get the following error.

java concatenation problem

Notice that it says the concatenation method can only take strings as an argument, not an integer value.

 

Method-3: Java string concatenation using StringBuilder class

This is again the most efficient way to concatenate java strings as this method does not create a new object of the resulting string that means it saves a lot of memory space. When using string builder for string concatenation, initialize string builder object with a specified capacity equal to a number of characters in the final concatenated string. This will result in efficient memory usage as well as save time spent during the resizing of characters. The simple syntax of the StringBuilder class for string concatenation would be;

StringBuilder NewStringBuilder = new StringBuilder();
NewStringBuilder.append(string);

Now let us take an example and see how we can add strings using the above method.

 

Example-1 Concatenation of two Strings using StringBuilder class

We already had discussed and learned the basic syntax of concatenating strings using the StringBuilder class Let us take an example and append two strings together and print the appended one. See the example below:

// java main class
public class Main{
	// java main method
	public static void main(String args[]){
		// creating string variables
		String string1= "Welcome to ";
		String string2= "Golinuxcloud";
		// create StringBuilder Instance
		StringBuilder newstring=new StringBuilder ();
		// using append method to concatenate strings
		StringBuilder concatenate=newstring.append(string1).append(string2);
		// printing
		System.out.println(concatenate);
	}
}

Output:

Welcome to Golinuxcloud

Notice that we successfully added the two strings using the above method. We can also add as many strings together as we want using the same method, just we need to add more by using appending method again and again.

 

Example-2 Java concatenate the string with other data types using the StringBuilder class

We can also add any other data type with a string using the StringBuilder class. All we need is to provide the value inside the append method. See the example below which appends integer value to the string value.

// java main class
public class Main{
	// java main method
	public static void main(String args[]){
		// creating variables
		String string1= "total class are: ";
		Integer string2= 9;
		// create StringBuilder Instance
		StringBuilder newstring=new StringBuilder ();
		// using append method to concatenate strings
		StringBuilder concatenate=newstring.append(string1).append(string2);
		// printing
		System.out.println(concatenate);
	}
}

Output:

total class are: 9

Notice that we get concatenated output that contains integer and string data types.

 

Method-4: Java string concatenation using StringBuffer class

Another efficient way to concatenate java strings is using String buffer for concatenating which also does not create a new object of the resulting string. Using String Buffer saves a lot of memory space as compared to using the + operator. The following is the simple syntax of the StringBuffer class.

StringBuffer NewStringBuffer = new StringBuffer();
NewStringBuffer.append(stringName);

Notice that the syntax is very much similar to the StringBuilder. Now let us take examples and append strings using this method.

 

Example-1 Concatenation of strings using StringBuffer class

Now we are familiar with the syntax of the StringBuffer class, let us now take an example and append two different strings together using this method. See the example below:

// java main class
public class Main{
	// java main method
	public static void main(String args[]){
		// creating variables
		String string1= "Welcome to ";
		String string2= "Golinuxcloud";
		// create StringBuffer Instance
		StringBuffer newstring=new StringBuffer();
		// using append method to concatenate strings
		StringBuffer concatenate=newstring.append(string1).append(string2);
		// printing
		System.out.println(concatenate);
	}
}

Output:

Welcome to Golinuxcloud

Notice that we added two stings together and printed the appended one.

 

Example-3 Java concatenate the string with other data types using the StringBuffer class

This method also allows adding any other data type with Java strings. For example, in the following program, we will add integer value to the string using the StringBuffer class. See the example below:

// java main class
public class Main{
	// java main method
	public static void main(String args[]){
		// creating variables
		String string1= "Total classes are: ";
		Integer string2= 4;
		// create StringBuffer Instance
		StringBuffer newstring=new StringBuffer();
		// using append method to concatenate strings
		StringBuffer concatenate=newstring.append(string1).append(string2);
		// printing
		System.out.println(concatenate);
	}
}

Output:

Total classes are: 4

Notice that we successfully added the integer value to the string and printed it out.

 

Summary

The string concatenation operation produces a new String by appending the second operand onto the end of the first operand. In this tutorial, we learned about java string concatenation. We learned four different methods in java to append strings together using various examples. The methods include StringBuffer class, Plus operator, concat() method, and StringBuilder class.

We take various examples and append strings with strings and strings with other data types using the mentioned methods. All in all, this tutorial contains all the necessary information and examples that you need to know in order to start working with appending java strings.

 

Further Reading

Java string concatenation
Java StringBuilder class
Java StringBuffer class

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment