Last Updated on May 7, 2020 by Scientech Easy
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.
Best ways to concatenate Strings in Java
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 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.
String s1 = "Dhan"; String s2 = "bad"; String s3 = s1.concat(s2);
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
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.
Explanation of output:
As you can see in the above figure, still ‘s’ is pointing to “Hello” only. Therefore, the output will be Hello.
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
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.
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.
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
String str = 25 + "July" + 1987;
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
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 ⇒