After a string literal, all + will be considered as a string concatenation operator, why?

I did not understand the reason why an integer is considered as a string literal in concatenation. For instance.

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

Exit: 40 Sachin 4040 .

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

+5
source share
5 answers

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

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

The following + operators receive the String operand and the 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 bring out 40 Sachin 80 .

+8
source

Since it is implemented, because + is used to add numbers, as for string concatenation.

For the first time, none of the parts is a string, but both are numeric values ​​that can be added, so they were used to add values.

But, as soon as one part of the two is a string, it is used to concatenate the strings.

Change your code as follows:

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

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

The Java programming language ensures that the operands of the operators are apparently 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 behavior of + when one of the operands is String is specified in section 15.18.1 on "Concatenating Strings" Operator + ":

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

+3
source

The complement remains associative. see 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 ----------------- 
+2
source

There are two types of "+" operators in the operator, one of the String objects and one of the Integer object:

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

First the operation "10 + 30" is applied. Let's look at the value "10", this is interpreted by the Integer object, which activates the + operator by the value "30", which is obtained as an integer 40.

The next operation is the last result of 40 with "Sachin": 40 + "Sachin". An Integer object that activates the + operator on a String object that outputs the String object "40 Sachin".

The next operation is the last String String ('40 Sachin ') object, which adds an Integer 40, which results in a "40 Sachin 40".

In the same way, adding the last β€œ40”, which ultimately results in a '40 Sachin 4040 '.

0
source

All Articles