Table of Contents

In java, to concatenate two strings we can use + (String concatenation) operator or concat() of the String class or StringBuilder class or StringJoiner class ( from java 8 version) or String.join() method. All these method can concatenate the two Strings in Java.

For example, For given to input strings: 

string1 = Welcome to
string2= Interview Expert.

Then the resulting output should be, Welcome to Interview Expert.

In this article we will learn to concatenate two strings in java through examples, detailed logic and program explanation for better understanding.

Required Knowledge

  • Java programming basics
  • Java Operators
  • String class methods
  • StringBuilder and StringJoiner

Problem Statement

We need to write a java program to concatenate two strings. For a given two input strings, the program should concatenate (add second string to the first string end) them. This action will be done in our program. Let’s see an example,

Example:

For a given two input strings, “Hi”, “Namastey”, Output should be “Hi, Namastey”.

Explanation:

First input String str: "Hi"

Second input String str1: "Namastey"

Concatenation of two strings is:

str+str1 = Hi, Namastey

Hence, after concatenating two strings, then the result will be "Hi, Namastey".

Logics to concatenate two strings

1. Using + operator:

  • In order to concatenate two strings using ‘+’ operator, we first need to initialize the two strings (str and str1) by asking the user.
  • Then concatenates the second string str1 to the end of the first string str, separated by a space and stores in str using + operator.
  • Then print str to display the result.

2. Using concat() method:

  • In order to concatenate two strings using concat() method, we first need to initialize the two strings (str and str1) by asking the user.
  • The concat() method of the String class is used to concatenate str1 to the end of str, with a space in between.
  • The concatenated string is stored in a new variable named str3. So the str3 holds concatenated data.
  • And print str3 to display the result.

3. Using StringBuilder:

  • In order to concatenate two strings using StringBuilder class, first we need to create a StringBuilder object as sb.
  • Then append strings to the StringBuilder object sb using the append() method, strings are appended sequentially to build the concatenated result within the StringBuilder.
  • The toString() method is called on the StringBuilder object to convert the concatenated result into a string and print the result.

4. Using StringJoiner:

  • In order to concatenate two strings using StringJoiner class, first we need to create a StringJoiner object as sj.
  • Then add strings to the StringJoiner object sj using the add() method, strings are added sequentially to build the concatenated result within the StringJoiner.
  • The toString() method is called on the StringJoiner object to convert the concatenated result into a string and print the result.

5. Using String.join() method:

  • In order to concatenate two strings using String.join() method, we first need to initialize the two strings (str and str1) by asking the user.
  • The String.join() method is used to concatenate the two input strings.
  • This method takes three parameters: the delimiter (in this case, an empty string), the first string (str), and the second string (str1), separated by a space.
  • The concatenated string is stored in a variable named str2. And print this variable str2 to display the result.

Java program to concatenate two strings

✅ Program 1: Using + operator

In the below program we are using + operator to concatenate two strings.

import java.util.Scanner;
public class ConcatenateStrings {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a text");
		String str = s.nextLine();
		System.out.println("Enter another text");
		String str1 = s.nextLine();
		str +=" "+ str1;
		System.out.println(str);
		s.close();
	}
}

Output:

Enter a text
Happy
Enter another text
World
Happy World

🧠 Explanation:

  • The program asks the user to enter two text strings.
  • It reads the input strings using a Scanner object and stores them in variables str and str1.
  • The program concatenates the two input strings by appending str1 to the end of str, separated by a space.
  • The concatenated string is then printed to the console.
  • Finally, the Scanner object s is closed to release system resources.

✅ Program 2: Using concat() method

In the below program we are using concat() method to concatenate two strings.

import java.util.Scanner;
public class ConcatenateStrings {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a text");
		String str = s.nextLine();
		System.out.println("Enter another text");
		String str1 = s.nextLine();
		String str3 = str.concat(" " +str1);
		System.out.println(str3);
		s.close();
	}
}

Output:

Enter a text
I love
Enter another text
Tea
I love Tea

🧠 Explanation:

  • The program asks the user to enter two text strings.
  • It reads the input strings using a Scanner object and stores them in variables str and str1.
  • It concatenates the second string str1 to the end of the first string str, separated by a space, using the concat() method.
  • The concatenated string is stored in a new variable str3.
  • The concatenated string str3 is then printed to the console.
  • Finally, the Scanner object s is closed to release system resources.

✅ Program 3: Using StringBuilder

In the below program we are using StringBuilder class to concatenate two strings.

public class ConcatenateStrings {
	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder() ;
			sb.append("Hi,");
			sb.append(" ");
			sb.append("How are you??");	
		System.out.println("Concatenation of two strings: "+sb.toString());
	}
}

Output:

 Concatenation of two strings: Hi, How are you??

🧠 Explanation:

  • The program defines a class named ConcatenateStrings.
  • Inside the class, there’s a main method, the entry point of the program.
  • It creates a new StringBuilder object named sb to efficiently manipulate strings.
  • The program appends the string “Hi,” to the StringBuilder object using the append() method.
  • It appends a space character to the StringBuilder object.
  • It appends the string “How are you??” to the StringBuilder object.
  • It converts the content of the StringBuilder object to a String using the toString() method.
  • Finally, it prints the concatenated string to the console using System.out.println().

✅ Program 4: Using StringJoiner

In the below program we are using StringJoiner class to concatenate two strings.

import java.util.StringJoiner;
public class ConcatenateStrings {
	public static void main(String[] args) {
		StringJoiner sj = new StringJoiner(",") ; //java 8 version
			sj.add("Hi");
			sj.add(" How are you??");	
		System.out.println("Concatenation of two strings: "+sj.toString());
	}
}

Output:

 Concatenation of two strings: Hi, How are you doing??

🧠 Explanation:

  • The program imports the StringJoiner class from java.util.
  • It defines a class named ConcatenateStrings.
  • Inside the class, there’s a main method, which is the entry point of the program.
  • It creates a new StringJoiner object named sj to join strings with a delimiter.
  • The delimiter specified for the StringJoiner is ,.
  • The program adds the string “Hi” to the StringJoiner object using the add() method.
  • It adds the string ” How are you??” to the StringJoiner object.
  • Finally, it converts the content of the StringJoiner object to a String using the toString() method and prints the concatenated string to the console using System.out.println().

✅ Program 5: Using String.join() method

In the below program we are using String.join() method to concatenate two strings.

import java.util.Scanner;
public class ConcatenateStrings {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a text");
		String str = s.nextLine();
		System.out.println("Enter another text");
		String str1 =s.nextLine();
		String str2 = String.join("",str," " ,str1);
		System.out.println(str2.toString());
        s.close();
	}
}

Output:

Enter a text
Hello
Enter another text
World!!
Hello World!!

🧠 Explanation:

  • The program asks the user to enter two text strings.
  • It reads the input strings using a Scanner object and stores them in variables str and str1.
  • It concatenates the two input strings by using the String.join() method. This method takes a delimiter (in this case, an empty string), followed by the strings to be concatenated.
  • The concatenated string is stored in a variable named str2.
  • The value of str2, representing the concatenated result, is printed to the console.
  • Finally, the Scanner object s is closed to release system resources.

Conclusion

The java programs we did here gives us concatenation of two strings using + operator, concat() method, StringBuilder, StringJoiner and String.join () method.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Categorized in:

Java Coding,