Java mysterious conclusion from additions: some are evaluated, some are printed

I was doing java assignment and I came across this code.

int x=3, y=5;
System.out.println(x + y + " = " + y + x);

      

and the output is "8 = 53". Why does the first x + y get evaluated and the last y and x expressions are output? Left me surprised. Thanx advance guys.

+3


source to share


5 answers


Remember that in Java an operator (for example +

) can be overloaded. This means that it will do different things depending on its operands. For +

there are (at least) two options: integer append and string concatenation.

The choice of overload therefore depends on the left operand. Also, concatenating strings with a non-kernel operand may result in automatic conversion to a string.

This will all be evaluated from left to right like this:



x + y + " = " + y + x

3 + 5 + " = " + 3 + 5     // 3+5 chooses the integer addition overload of +

8 + " = " + 3 + 5         // 8 + " = " chooses string concatenation
"8" + " = " + 3 + 5       // so 8 is converted to "8" first
"8 = " + 3 + 5            // and then concatenated with " = "

"8 = " + "3" + 5          // now 3 is converted to "3"
"8 = 3" + 5               // and concatenated with "8 ="

"8 = 3" + "5"             // finally 5 is converted to "5"
"8 = 35"                  // and concatenated with the rest

      

FWIW, this ambiguity makes me resent implicit conversions 1 .

1 - In C #. I like this in Python :-)

+6


source


the output is "8 = 53". Why is the first x + y getting evaluated and the last being the expression y and x?

Since the former are x + y

not appended to the string, so an integer computation is performed. The second is added to the line due " = " +

and hence they are printed as separate values.

To prove this, simply:



    System.out.println("" + x + y + " = " + x + y);

      

and the output will be:

35 = 35

+5


source


The first part is interpreted as integer addition, but in the second part (since you entered a string) it is interpreted as string concatenation as well.

Assuming you want 8 = 8

try

System.out.println(x + y + " = " + (y + x));

      

if you want to 3 + 5 = 5 + 3

then i would use String.format

both in

 System.out.println (String.format ("%d + %d = %d + %d", x, y, y, x));

      

+2


source


The reason for exiting "8 = 53" is as follows:

x + y + " = " + y + x

      

Calculated from left to right, so if we break it down piece by piece:

x + y = 8

8 + " = " = "8 = "

"8 = " + y = "8 = 5"

"8 = 5" + x = "8 = 53"

      

This is how the compiler gets your answer :)

+2


source


The first + is between two numbers, so the result is eight. The latter have strings on either side of them, so the numbers are converted to strings and concatenated together. The plus operator snaps itself closest to the left side and is evaluated from left to right. If you want the last addition to be numeric, the expression must be in parentheses ().

+1


source







All Articles