3

I was doing java assignment and I ran into 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 gets evaluated and the last y and x expression gets printed? Left me wondering. Thanx in advance guys.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • See also http://stackoverflow.com/questions/2955640/how-does-operator-behaves-differently-with-numbers-and-strings-in-java – Raedwald Oct 02 '14 at 07:10
  • See also http://stackoverflow.com/questions/3848250/system-out-printlnhi610-prints-hi610 – Raedwald Oct 02 '14 at 07:16

5 Answers5

6

Remember that in Java, an operator (like +) can be overloaded. That means it will do different things, depending on its operands. For +, there are (at least) two choices: integer addition and string concatenation.

Which overload is chosen depends more so on the left-hand-side operand. Also, string concatenation with a non-string operand can cause automatic conversion to a string.

The whole thing will be evaluated 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, it's ambiguity like this that leads me to dislike implicit conversions1.

1 - In C#. I love it in Python :-)

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
5

output is "8=53". Why does the first x+y gets evaluated and the last y and x expression gets printed?

Because first x + y are not appended with a string so integer calculation is done. Second ones are appended to a string because of " = " + and hence they are printed as individual values.

To prove this just do:

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

and the output will be:

35 = 35

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

The first part is getting interpreted as integer addition, but in the second part (as you have introduced a string) it is getting interpreted and string concatenation.

Assuming you want 8 = 8

try

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

if you want 3 + 5 = 5 + 3

then I would use String.format as in

 System.out.println (String.format ("%d + %d = %d + %d", x, y, y, x));
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

The reason the output is "8 = 53" is because:

x + y + " = " + y + x

Is 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"

That is how the compiler gets your answer :)

Stephen Buttolph
  • 643
  • 8
  • 16
1

The first + is between two numbers so the result is eight. The 2nd have strings on either side of them so numbers get converted to strings and concatenated together. The plus operator binds tightest to the left, and gets evaluated left to right. If you wanted the last addition to be numerical then the expression should be in brackets ( ).

AndrewN
  • 701
  • 3
  • 10