giri
giri

Reputation: 27199

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

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 anybody tell me how do I get 1711?

Upvotes: 5

Views: 7118

Answers (10)

Carl Norum
Carl Norum

Reputation: 224944

The 17 is there directly.

013 is an octal constant equal to 11 in decimal.

013 = 1*8 + 3*1 = 8 + 3 = 11

When added together after a string, they are concatenated as strings, not added as numbers.

I think what you want is:

int x = 17;
int y = 013;
int z = x + y;

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

or

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

Which will be a better result.

Upvotes: 11

Danny Varod
Danny Varod

Reputation: 18068

"x+y = " + x+y

equals

("x+y = " + x) + y

which equals

("x+y = " + String.valueOf(x)) + y

which equals

"x+y = " + String.valueOf(x) + String.valueOf(y)

013 is octal = 11 in decimal

Upvotes: 2

polygenelubricants
polygenelubricants

Reputation: 383746

There are two issues here: octal literal, and order of evaluation.

int y = 013 is equivalent to int y = 11, because 13 in base 8 is 11 in base 10.

For order of evaluation, the + operator is evaluated left to right, so "x+y = " + x+y is equivalent to ("x+y = " + x)+y, not "x+y = " + (x+y). Whitespaces are insignificant in Java.

Look at the following diagram (s.c. is string concatenation, a.a. is arithmetic addition):

("x+y = " + x)+y
          |   |
     (1) s.c  |
              |
             s.c. (2)


"x+y = " + (x+y)
         |   |
         |  a.a. (1)
         |
        s.c. (2)

In both diagrams, (1) happens before (2).

Without the parantheses, the compiler evaluates left-to-right (according to precedence rules).

 "x+y = " + x+y
          |  |
         (1) |
             |
            (2)

Upvotes: 8

mohdajami
mohdajami

Reputation: 9680

everyone mentioned 0 makes the number octal.
I just want to add that 0x makes it hexadecimal

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

I would just add to Reed's explanation that 013 is interpreted as Octal 13, which is decimal 11.

Upvotes: 0

brettkelly
brettkelly

Reputation: 28205

It appears to be interpreting y as using octal notation (which evaluates to 11). Also, you're concatenating the string representations of x and y in System.out.printLn.

Upvotes: 0

David Johnstone
David Johnstone

Reputation: 24450

It's string concatenation of decimal 17 and octal 13 (equivalent to decimal 11).

Upvotes: 0

tanascius
tanascius

Reputation: 53944

013 is ocatal ... convert it to decimal and you will get 11. Have a look here for an explanation of the octal system.

Upvotes: 0

Michael Stum
Michael Stum

Reputation: 180944

Numbers prefixed with 0 are octal. 13 Octal is 11 decimal.

The x + y call treats both paramters as strings, so you are combining the string "17" and "11".

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

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

Upvotes: 2

Related Questions