1

Mathematical Expression

  • 1 + + 1 results 2
  • 1 + - 1 returns 0
  • 1 + - + 1 returns 0

can anybody know the reason of this. because I only know ++ -- operation but in this case the operator is '+ +' and still not giving an error.

Ashish Aggarwal
  • 2,980
  • 2
  • 22
  • 45

2 Answers2

14

Its because + is also a unary operator which means positive, just like - means negative.

1 + + 1 =   1 + (+1)  = 1 + 1  = 2
1 + - 1 =   1 + (-1)  = 1 - 1  = 0
1 + - + 1 = 1 + -(+1) = 1 + -1 = 1 - 1 = 0;
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
7

Unary + and - operators at work here.

1 + (+1) = 2
1 + (-1) = 0
1 + (-(+1)) = 0

JLS §15.15.3

Anirudh Ramanathan
  • 45,145
  • 22
  • 127
  • 184
  • See also: http://stackoverflow.com/questions/2624410/what-is-the-purpose-of-javas-unary-plus-operator – slim May 19 '14 at 12:15