Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Concatenate Two Strings in Java Without Using + Operator

String concatenation means joining two strings to form a single string. Although Java provides the + operator and concat() method, learning to concatenate strings manually helps understand how strings work internally.

In this program, we concatenate two strings by:

How the Logic Works

  1. Read two input strings.
  2. Create a character array with combined length.
  3. Copy characters of the first string.
  4. Append characters of the second string.
  5. Convert the character array into a string.

Example 1

String 1: "Hello"

String 2: "World"

Result: HelloWorld

Example 2

String 1: "Java "

String 2: "Programming"

Result: Java Programming

Java Program to Concatenate Two Strings Without Using +


import java.util.Scanner;

public class ConcatenateStrings {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first string: ");
        String str1 = sc.nextLine();

        System.out.print("Enter second string: ");
        String str2 = sc.nextLine();

        char[] result = new char[str1.length() + str2.length()];

        int index = 0;

        for (int i = 0; i < str1.length(); i++) {
            result[index++] = str1.charAt(i);
        }

        for (int i = 0; i < str2.length(); i++) {
            result[index++] = str2.charAt(i);
        }

        String finalString = new String(result);

        System.out.println("Concatenated String: " + finalString);
    }
}

Sample Output


Enter first string: Good
Enter second string: Morning
Concatenated String: GoodMorning

Important Notes

Practice Challenges

  1. Concatenate multiple strings using the same logic.
  2. Concatenate two strings without using arrays.
  3. Insert a space automatically between the two strings.

Manual string concatenation strengthens your understanding of memory handling and string processing in Java.