How to Concatenate Two Strings in Java
How to Concatenate Two Strings in Java
Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate two strings in Java are mentioned below.
By Using + Operator
The + operator can be used to concatenate the strings. Let’s understand it with the help of the following example.
FileName: StringConcatenationExample.java
public class StringConcatenationExample { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Doing the concatenation work str1 = str1 + " – A Good Site To Learn Java."; System.out.println(str1); } }
Output:
Tutorial & Example – A Good Site To Learn Java.
Explanation: By looking at the output, it is evident that the + operator concatenates the strings “Tutorial & Example” and “– A Good Site To Learn Java.”
By Using the concat() Method
The concat() method can also be used to do the concatenation work.
Syntax:
public String concat (String anotherStr)
String anotherStr: represents the string that is being concatenated with the current string.
A String is returned, which is the concatenation of the current string and anotherStr.
The following program illustrates the same.
FileName: StringConcatenationExample1.java
public class StringConcatenationExample1 { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Doing the concatenation work str1 = str1.concat(" – A Good Site To Learn Java."); System.out.println(str1); } }
Output:
Tutorial & Example – A Good Site To Learn Java.
Explanation: In the above program, the + operator is replaced by the concat() method to accomplish the concatenation work. Everything else remains the same. Hence, the output also remains the same.
By Using the append() Method
Another approach is to use the append() method to do the concatenation of the given strings. However, the append() method is not present in the Java String class. Therefore, to apply the append() method, one needs the object of either StringBuilder or StringBuffer class. Observe the following program.
FileName: StringConcatenationExample2.java
public class StringConcatenationExample2 { // main method public static void main(String argvs[]) { // Input string String str1 = "Tutorial & Example"; // Creating an object of the StringBuilder class StringBuilder strBldrObj = new StringBuilder(str1); // Doing the concatenation work strBldrObj = strBldrObj.append(" – A Good Site To Learn Java."); // getting the string from the // object of the StringBuilder class str1 = strBldrObj.toString(); // displaying the string System.out.println(str1); str1 = "Let's visit"; // Creating an object of the StringBuffer class StringBuffer strBfrObj = new StringBuffer(str1); // Doing the concatenation work strBfrObj = strBfrObj.append(" The Tutorial & Example Site Again To Learn Java."); // getting the string from the // object of the StringBuffer class str1 = strBfrObj.toString(); // displaying the string System.out.println(str1); } }
Output:
Tutorial & Example – A Good Site To Learn Java. Let's visit The Tutorial & Example Site Again To Learn Java.
Explanation: The above program shows that either StringBuilder or StringBuffer class can be used to do the concatenation of string. The concatenation of strings is achieved by invoking the append() method on the objects of the classes StringBuilder or StringBuffer. The toString() method then used to extract the concatenated string from those objects, which is displayed in the output.