Java String Concatenation | Java Concat Method

String Concatenation in Java


In general, Java does not allow operators to be applied to string objects except + operator which concatenates multiple strings and forms a new string.

A string in Java can be concatenated with two or more strings or a string with a number or symbol to display information as per requirements.

For example, when you fill an online registration form, you fill your first name, middle name, and last name in three separate columns. But when you finish the form then you see your complete name as a single string. This is a realtime use of Java string concatenation.

Best ways to concatenate Strings in Java


 
There are two best ways to concatenate strings in Java. They are as follows:
1. By concat() method.
2. By + (String concatenation) operator.

String Concatenation by concat() method in Java


This string concat() method concatenates or joins the specified string to the end of current string and creates a new string object. The general form of this method is as follows:

public String concat(String str)  // Here, 'concat' is a method name and return type of this method is string object.
‘String str’ in the parenthesis represents that you are passing another string object or string values to concat() method while it is called. 
For example:
String s2 = s1.concat("Hi");

The concat() method will concatenate or join String “Hi” at the end of string class object s1 and return the second string s2 as a result. Here, s1 is a string class object.

Similarly, if
String s1 = "Dhan"; 
String s2 = "bad";
String s3 = s1.concat(s2);
then we can expect s3 will be “Dhanbad”. 
Let’s see a simple example program to understand the concept.
Program source code 1:
package stringPrograms; 
public class StringConcatenationTest1 
{ 
public static void main(String[] args) 
{ 
 String s1 = "Hello";
 s1.concat("Java"); 
 System.out.println(s1); // Hello 
 } 
}
Output: 
        Hello
Explanation of memory concept: 
String concatenation in Java

1. When line 6 is executed by JVM, a string object will create in the string constant pool and stores “Hello” in it. The reference variable s1 is pointing to that object.

2. As you know that once we create a string object, we are not allowed to perform any changes in that object. If you will try to change, a new object will be created with these changes in the heap area.

When the statement s1.concat(“Java”); will be executed, Java will be added at the end of “Hello” and it will become “Hello Java”. Since we performed changes in the existing object. That’s why a new object will be created in the heap area and stores “Hello Java” in it.

 

But we did not assign any reference variable to this new object, therefore, it will be removed from the memory by the garbage collector.

 

3. When line 6 will be executed, for every string literal, one copy of object will also be created in the string constant pool for further usability and stores “Java” in it.

Explanation of output: 
As you can see in the above figure, still ‘s’ is pointing to “Hello” only. Therefore, the output will be Hello.
Program source code 2:
package stringPrograms; 
public class StringConcatenationTest2 
{ 
public static void main(String[] args) 
{ 
 String s1 = "Indian "; 
 String s2 = "Team"; 
 String s3 = s1.concat(s2); 
 System.out.println(s3); // Indian Team 
 } 
}
Output: 
      Indian Team
Explanation of memory concept:
String Concatenation by concat() method in Java

As you can see in the above figure, total of three objects is created, one in heap area and two in the SCP area.


Program source code 3:
Consider the following program.
package stringPrograms; 
public class StringConcatenationTest1 
{ 
public static void main(String[] args) 
{ 
 String s1 = new String("Java");
  s1.concat(" Core"); 
  s1 = s1.concat(" Programming"); 
 System.out.println(s1); 
 } 
}

1. How many string objects will be created in the heap area and string constant pool area?
2. What will be the output of the following program?

A. To find out string objects and output of the above program, let’s understand memory concept from the below figure.

Allotting memory for string objects

1. a) When line 6 will be executed by JVM, one object will be created in the heap area and one copy of object will be created in the SCP area for future purposes. The reference variable s1 is pointing to object “Java” in the heap area.

b) When line 7 will be executed by JVM, for every string literal, one copy of object ” Core” will be created in the SCP area for future purposes. The content ” Core” will be added at the end of “Java” and it will be stored in the heap as “Java Core”.

Since we are not pointing any reference variable to this new object. Therefore, the garbage collector will remove this newly created object from the memory.

c) When JVM will execute line 8, for string literal, one copy of the object will create in the SCP area and stores ” Programming” in it. JVM will concatenate content ” Programming” to the end of “Java” and it becomes “Java Programming”.

After concatenation, JVM will create a new object and store string “Java Programming” in that object. But the content of string s1 is not modified. That’s why strings are called immutable.

Now, JVM will point reference s1 towards that new object because we are assigning the same reference variable s1 to newly created object.  The old object that contains “Java”, has lost its reference as shown in the figure and the garbage collector will remove it from the memory.

Thus, as you can observe from the figure that a total of 6 objects are created in the memory, three objects in the heap and three objects in the string constant pool. 

2. The output of the program will be Java Programming because s1 is pointing to newly created object in the heap.

String Concatenation in Java using + operator


The operator (+) is used to add two or more strings in Java. For example, two strings “Hello” and ” Java” are concatenated to form a single string Hello Java.
     String str = “Hello” + ” Java”;

Let’s see an example program based on this concept.
Program source code 4:

package stringPrograms; 
public class StringConcatenationTest4 
{ 
public static void main(String[] args) 
{ 
 String s1 = "Shubh" + " Deep"; 
 System.out.println(s1); // Shubh Deep 
 } 
}
Output: 
       Shubh Deep

Java String Concatenation with Other Data Types


In Java, String values can also be easily concatenated with primitive values using double equal operator. For example,
String str = 25 + "July" + 1987;
Program source code 5:
package stringPrograms; 
public class StringConcatenationTest5 
{ 
public static void main(String[] args) 
{ 
 int date = 15; 
 String month = " August "; 
 int year = 1947; 
 String s1 = date + month + year; 
 System.out.println(s1); // 15 August 1947 
 } 
}
Output: 
       15 August 1947
In the above example program, two variables, date and year with data type int are concatenated along with a String object “month”. The variable date is initialized with a value 15 while the variable year is initialized with a value 1947. The string object month is initialized with the value of August. After concatenation, the result is 15 August 1947.

Final words 
Hope that this tutorial has covered almost all the important points related to Java string concatenation with example programs and memory concept. I hope that you will have understood and enjoyed it.
Thanks for reading!!!
Next ⇒ Substring in Java⇐ PrevNext ⇒

Leave a Comment