In Java/C++/C ++
is not same as + +
. ++
/--
are the Increment/Decrement operator.
The first case does not work because it does not apply to literals (1
or 2
).
Even then it would not be a valid statement, Neither 1++ 2
nor 1 ++2
are valid statement in Java. The second example works because it is interpreted as 1 + (+2)
. The Java lexer ignores white space. In the same way this is valid :
1 + + + 2 --> 1 + (+ (+2))
Or
1 + + + + + + + + + + + + + + + + + + + + + + + + + + + 2
It works only because +
is a unary
operator. It does not work for strings as below :
"a" + + "b" // does not work because +"b" is not valid.
Similarly it is not valid with multiplication
1 * * 2 // does not work because *2 is not valid.