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
  • 3,018
  • 2
  • 23
  • 46

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
  • 525,659
  • 79
  • 751
  • 1,130
7

Unary + and - operators at work here.

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

JLS §15.15.3

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