Java String Concatenation | Java Concat Method - Scientech Easy

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 you finish the form then you see your complete name as a single string. This is the practical use of Java string concatenation.

There are two ways to concat string in Java.
1. By concat() method.
2. By + (String concatenation) operator.

String Concatenation by concat() method


This string concat() method concatenates or joins the specified string to the end of the current string and creates a new string object. The general form of this method is given below.
      public String concat(String str)  // Here, 'concat' is a method name and the 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 the concat() method while it is called. 
For example:
   String s2=s1.concat("Hi");
The concat() method will concatenate or join the String “Hi” at the end of the String class object s1 and returns the second string s2 as a result. Here, s1 is a string class object.
Similarly, if String s1="Dhan"; String s2="bad"; and 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"; // Line 4 s1.concat("Java"); // Line 5 System.out.println(s1); // Hello } }
    Output: Hello
 Explanation of memory concept of this program: 
Java String concatenation
1. When line 4 is executed by JVM, a string object will create in the string constant pool and stores "Hello" in it and 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 4 will be executed, for every string literal, one copy of the object will also be created in the string constant pool for further usability and stores "Java" in it.

Explanation of the output of the above program: 
As you can see in the above figure, still 's' is pointing to the "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 of this program: 
concat() method
As you can see in the above figure that total of three objects is created, one in the 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"); // Line 4 s1.concat(" Core"); // Line 5 s1=s1.concat(" Programming"); // Line 6 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 the string objects and output of the above program, let's understand the memory concept from the below figure.
Java string concatenation
1. a) When line 4 will be executed by JVM, one object will be created in the heap area and one copy of the object will be created in the scp area for future purpose. The reference variable s1 is pointing to the object "Java" in the heap area.
b) When line 5 will be executed by JVM, for every string literal, one copy of the object "Core" will be created in the scp area for future purpose. Now, "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 6, for string literal, one copy of the object will create in the scp area and stores " Programming" in it. Now JVM will concatenate the content " Programming" to the end of "Java" and it becomes "Java Programming". After concatenation, JVM will create a new object and stores the string "Java Programming" in that object. But the content of the string s1 is not modified. That's why strings are called immutable.

Now JVM will point the reference s1 towards that new object because we are assigning the same reference variable s1 to the 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 see 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 the newly created object in the heap.

String Concatenation by + operator


The operator (+) is used to add two or more strings in Java. For example, the two strings "Hello" and "Java" are concatenated to form a single string HelloJava.
     String str="Hello" + "Java";
Let's see one example program related to 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

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 practical example programs and memory concept. I hope that you will have enjoyed this article.
Thanks for reading!