Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After a string literal, all the + will be treated as string concatenation operator why? [duplicate]

Tags:

java

I haven't understood the reason why integer is treated as string literal in concatenation. E.g.

String s=10+30+" Sachin "+40+40;  
System.out.println(s);

The output is: 40 Sachin 4040.

Why 40+40 is not getting added and why 10+30 is getting added?

like image 640
Shekhar Khairnar Avatar asked Jan 04 '16 10:01

Shekhar Khairnar


4 Answers

The expression is evaluated left to right. The first two operands are both int (10 and 30), so the first + performs addition.

The next + gets an int operand (40) and a String operand (" Sachin "), so it converts the int to String and performs String concatenation.

The next + operators get a String operand and an int operand, and also perform String concatenation.

If you want a different evaluation order, use parentheses :

String s=10+30+" Sachin "+(40+40);  

This will output 40 Sachin 80.

like image 182
Eran Avatar answered Oct 11 '22 12:10

Eran


This is because Java evaluates operands from left to right. Quoting section 15.7:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

String s=10+30+" Sachin "+40+40;
//       ^^^^^ + the operands are of type int so int addition is performed
String s=40+" Sachin "+40+40;
//       ^^^^^^^^^^^^^ + the operands are of type int and String so String concatenation is performed
String s="40 Sachin "+40+40;
//       ^^^^^^^^^^^^^^^ + the operands are of type int and String so String concatenation is performed
String s="40 Sachin 40"+40;
//       ^^^^^^^^^^^^^^^^^ + the operands are of type int and String so String concatenation is performed
String s="40 Sachin 4040";

The behaviour of + when one of the operands is a String is specified in section 15.18.1 about the "String Concatenation Operator +":

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.

like image 29
Tunaki Avatar answered Oct 11 '22 14:10

Tunaki


Because that's how it is implemented, because + is both used for adding numbers, as for String concatenation.

The first time, neither of the parts is a String, but both are numerical values that can be added, so it's used to add the values.

But, as soon as one part of the two is a String, it is used for String concatenation.

Change your code like this:

String s=10+30+" Sachin "+(40+40); 
like image 42
Stultuske Avatar answered Oct 11 '22 12:10

Stultuske


Addition is left associative. let's see it step by step

10+30+" Sachin "+40+40
-----           -------
40 +" Sachin "+40+40    <--- here both operands are integers with the + operator, which is addition
---
"40 Sachin "+40+40   <--- + operator on integer and string results in concatenation
-----------
"40 Sachin 40"+40   <--- + operator on integer and string results in concatenation
--------------
"40 Sachin 4040"   <--- + operator on integer and string results in concatenation
-----------------
like image 27
Let'sRefactor Avatar answered Oct 11 '22 12:10

Let'sRefactor