Java: sum of two integers being printed as concatenation of the two

前端 未结 10 2049
忘了有多久
忘了有多久 2020-12-03 19:41

Consider this code:

int x = 17;
int y = 013;
System.out.println(\"x+y = \" + x + y);

When I run this code I get the output 1711. Can anybod

10条回答
  •  心在旅途
    2020-12-03 19:55

    You're doing string concatenation in the final print, since you're adding to a string. Since "x+y = " is a string, when you add x to it, it's giving you "17", making "x+y = 17".

    THe 013 is in octal, so treated as a decimal, you get 11. When you concatenate this, you end up with "x+y = 17" + "11", or 1711

提交回复
热议问题