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);
Output
John Doe
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);
Output
30
System.out.println("Java" + " Programming");
Output
Java Programming
System.out.println("Version " + 21);
Output
Version 21
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);
Output
rajesh102030
60rajesh
30rajesh30
10rajesh2030
Expression 1
a + b + c + d
Evaluation
"rajesh" + 10
↓
"rajesh10"
↓
"rajesh1020"
↓
"rajesh102030"
Output
rajesh102030
Since the first operand is a String, the remaining operations are concatenations.
Expression 2
b + c + d + a
Evaluation
10 + 20
↓
30
↓
30 + 30
↓
60
↓
60 + "rajesh"
↓
"60rajesh"
Output
60rajesh
All numeric additions happen first because no String has appeared yet.
Expression 3
b + c + a + d
Evaluation
10 + 20
↓
30
↓
30 + "rajesh"
↓
"30rajesh"
↓
"30rajesh30"
Output
30rajesh30
The first String changes all remaining operations into concatenation.
Expression 4
b + a + c + d
Evaluation
10 + "rajesh"
↓
"10rajesh"
↓
"10rajesh20"
↓
"10rajesh2030"
Output
10rajesh2030
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;
| 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
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]);
}
}
Run
java Test 10 20
Output
1020
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);
Output
30
The += Operator Is Also Overloaded
The += operator can also perform string concatenation.
Example
String s = "rajesh";
s = s.concat("software");
System.out.println(s);
Output
rajeshsoftware
The same result can be achieved using the + operator.
String s = "rajesh";
s = s + "software";
System.out.println(s);
Or more simply,
String s = "rajesh";
s += "software";
System.out.println(s);
Output
rajeshsoftware
Common Beginner Mistakes
Expecting Addition Instead of Concatenation
System.out.println("10" + 20);
Output
1020
Not
30
Forgetting Left-to-Right Evaluation
System.out.println(10 + 20 + "30");
Output
3030
Explanation
10 + 20 = 30
30 + "30"
↓
"3030"
System.out.println("10" + 20 + 30);
Output
102030
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));
Output
Total = 30
Without parentheses
System.out.println("Total = " + 10 + 20);
Output
Total = 1020
Quick Memory Trick 🧠
Once a
Stringenters 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
Stringappears, 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)