Supreet
Supreet

Reputation: 33

Arithmetic operator in java

I have encountered strange arithmetic operations and here is the code:

    int i = 9 + + 8 - - 11 + + 13 - - 14 + + 15;

    System.out.println(i);

It works without compilation error and gives the output 70, I have tried to google but did not find the correct explanations. Please pardon me I am new to Java.

Upvotes: 3

Views: 139

Answers (3)

Ramsharan
Ramsharan

Reputation: 2064

9+ +8 is equivalent to 9+(+8) and

8- -11 is equivalent to 8-(-11)

so 9 + + 8 - - 11 + + 13 - - 14 + + 15 is equivalent to 9+(+8)-(-11)+(+13)-(-14)+(+15)

which is equivalent to 9+8+11+13+14+15 = 70

Upvotes: 2

Nitesh Virani
Nitesh Virani

Reputation: 1712

Actually it is mathematical arithmetic operation and the same applies in Java:

- - = +

+ + = +

int i = 9 + 8 + 11 + 13 + 14 + 15;

so it is 70

Upvotes: 2

Eran
Eran

Reputation: 393771

int i = 9 + + 8 - - 11 + + 13 - - 14 + + 15;

is equivalent to

int i = 9 + (+8) - (-11) + (+13) - (-14) + (+15);

which is equivalent to

int i = 9 + 8 + 11 + 13 + 14 + 15;

which is equal to 70

Upvotes: 9

Related Questions