DEV Community

Cover image for String Concatenation Operator (`+`)
Rajesh Bhola
Rajesh Bhola

Posted on

String Concatenation Operator (`+`)

The + operator is one of the most interesting operators in Java because it serves two different purposes.

  • It performs arithmetic addition when both operands are numeric.
  • It performs String concatenation when at least one operand is a String.

This makes it the only overloaded operator in Java, making it a popular topic in Java interviews.

Let's understand how it works with simple examples.


What Is the String Concatenation Operator?

Concatenation means joining two or more strings together.

Example

String firstName = "John";
String lastName = "Doe";

System.out.println(firstName + " " + lastName);
Enter fullscreen mode Exit fullscreen mode

Output

John Doe
Enter fullscreen mode Exit fullscreen mode

Here, the + operator joins multiple strings into a single string.


The Only Overloaded Operator in Java

In Java, the + operator has two meanings.

Condition Behavior
Both operands are numeric Performs arithmetic addition
At least one operand is a String Performs string concatenation

Examples

System.out.println(10 + 20);
Enter fullscreen mode Exit fullscreen mode

Output

30
Enter fullscreen mode Exit fullscreen mode

System.out.println("Java" + " Programming");
Enter fullscreen mode Exit fullscreen mode

Output

Java Programming
Enter fullscreen mode Exit fullscreen mode

System.out.println("Version " + 21);
Enter fullscreen mode Exit fullscreen mode

Output

Version 21
Enter fullscreen mode Exit fullscreen mode

Since one operand is a String, Java converts the other operand into a string and performs concatenation.


Rule: Evaluation Happens from Left to Right

One of the most important rules is that the + operator is evaluated from left to right.

Once a String appears in the expression, every remaining + operation becomes string concatenation.

This is one of the most frequently asked Java interview concepts.


Famous Interview Example

String a = "rajesh";
int b = 10;
int c = 20;
int d = 30;

System.out.println(a + b + c + d);
System.out.println(b + c + d + a);
System.out.println(b + c + a + d);
System.out.println(b + a + c + d);
Enter fullscreen mode Exit fullscreen mode

Output

rajesh102030
60rajesh
30rajesh30
10rajesh2030
Enter fullscreen mode Exit fullscreen mode

Expression 1

a + b + c + d
Enter fullscreen mode Exit fullscreen mode

Evaluation

"rajesh" + 10
↓

"rajesh10"

↓

"rajesh1020"

↓

"rajesh102030"
Enter fullscreen mode Exit fullscreen mode

Output

rajesh102030
Enter fullscreen mode Exit fullscreen mode

Since the first operand is a String, the remaining operations are concatenations.


Expression 2

b + c + d + a
Enter fullscreen mode Exit fullscreen mode

Evaluation

10 + 20
↓

30

↓

30 + 30

↓

60

↓

60 + "rajesh"

↓

"60rajesh"
Enter fullscreen mode Exit fullscreen mode

Output

60rajesh
Enter fullscreen mode Exit fullscreen mode

All numeric additions happen first because no String has appeared yet.


Expression 3

b + c + a + d
Enter fullscreen mode Exit fullscreen mode

Evaluation

10 + 20
↓

30

↓

30 + "rajesh"

↓

"30rajesh"

↓

"30rajesh30"
Enter fullscreen mode Exit fullscreen mode

Output

30rajesh30
Enter fullscreen mode Exit fullscreen mode

The first String changes all remaining operations into concatenation.


Expression 4

b + a + c + d
Enter fullscreen mode Exit fullscreen mode

Evaluation

10 + "rajesh"

↓

"10rajesh"

↓

"10rajesh20"

↓

"10rajesh2030"
Enter fullscreen mode Exit fullscreen mode

Output

10rajesh2030
Enter fullscreen mode Exit fullscreen mode

Since the second operand is a String, concatenation begins immediately.


Assignment Examples

Given

String a = "rajesh";

int b = 10;
int c = 20;
int d = 30;
Enter fullscreen mode Exit fullscreen mode
Statement Result Reason
a = b + c + d; ❌ Compile-time error RHS is int, cannot assign to String
a = a + b + c; ✅ Valid RHS is String
b = a + c + d; ❌ Compile-time error RHS is String, cannot assign to int
b = b + c + d; ✅ Valid RHS is int

Typical compile-time error

incompatible types
found: java.lang.String
required: int
Enter fullscreen mode Exit fullscreen mode

Command-Line Arguments Example

Command-line arguments are always received as strings.

Example

class Test {

    public static void main(String[] args) {

        System.out.println(args[0] + args[1]);

    }

}
Enter fullscreen mode Exit fullscreen mode

Run

java Test 10 20
Enter fullscreen mode Exit fullscreen mode

Output

1020
Enter fullscreen mode Exit fullscreen mode

Many beginners expect the answer to be 30, but the output is 1020 because both command-line arguments are strings.

To perform arithmetic addition, convert them into integers.

int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

System.out.println(a + b);
Enter fullscreen mode Exit fullscreen mode

Output

30
Enter fullscreen mode Exit fullscreen mode

The += Operator Is Also Overloaded

The += operator can also perform string concatenation.

Example

String s = "rajesh";

s = s.concat("software");

System.out.println(s);
Enter fullscreen mode Exit fullscreen mode

Output

rajeshsoftware
Enter fullscreen mode Exit fullscreen mode

The same result can be achieved using the + operator.

String s = "rajesh";

s = s + "software";

System.out.println(s);
Enter fullscreen mode Exit fullscreen mode

Or more simply,

String s = "rajesh";

s += "software";

System.out.println(s);
Enter fullscreen mode Exit fullscreen mode

Output

rajeshsoftware
Enter fullscreen mode Exit fullscreen mode

Common Beginner Mistakes

Expecting Addition Instead of Concatenation

System.out.println("10" + 20);
Enter fullscreen mode Exit fullscreen mode

Output

1020
Enter fullscreen mode Exit fullscreen mode

Not

30
Enter fullscreen mode Exit fullscreen mode

Forgetting Left-to-Right Evaluation

System.out.println(10 + 20 + "30");
Enter fullscreen mode Exit fullscreen mode

Output

3030
Enter fullscreen mode Exit fullscreen mode

Explanation

10 + 20 = 30

30 + "30"

↓

"3030"
Enter fullscreen mode Exit fullscreen mode

System.out.println("10" + 20 + 30);
Enter fullscreen mode Exit fullscreen mode

Output

102030
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Remember that the + operator is evaluated from left to right.
  • If any operand is a String, the remaining operations become concatenation.
  • Convert numeric strings using Integer.parseInt() when arithmetic is required.
  • Use parentheses to make complex expressions easier to read.

Example

System.out.println("Total = " + (10 + 20));
Enter fullscreen mode Exit fullscreen mode

Output

Total = 30
Enter fullscreen mode Exit fullscreen mode

Without parentheses

System.out.println("Total = " + 10 + 20);
Enter fullscreen mode Exit fullscreen mode

Output

Total = 1020
Enter fullscreen mode Exit fullscreen mode

Quick Memory Trick 🧠

Once a String enters the expression (left to right), every remaining + becomes concatenation.

Remember:

  • Number + Number → Addition
  • String + Anything → Concatenation

Key Takeaways

  • The + operator is the only overloaded operator in Java.
  • It performs addition when both operands are numeric.
  • It performs concatenation when at least one operand is a String.
  • Java evaluates + expressions from left to right.
  • Once a String appears, every remaining + performs concatenation.
  • Command-line arguments are always strings.
  • The += operator also supports string concatenation.
  • Use parentheses whenever you want arithmetic to happen before concatenation.

Happy Coding!

Top comments (0)