The expression a---b not (as you expected) parsing as a-(-(-b)) , but rather as (a--) - b .
This example illustrates this:
int a = 0; int b = 0; System.out.println(a---b);
Given this behavior, a--b parsed as (a--)b , which is obviously a mistake.
When you put a space between the minuses, a- -b it is no longer parsed as a -- operator, but as a binary and unary minus: a - (-b) .
Note that you can write a- - -b , which is interpreted as a-(-(-b)) .
So why is this interpreted like this? Good @EJP gave a great comment on another answer. In JLS, section 3.2, you can read the following:
The longest translation is used at every step, even if the result does not ultimately make the right program, while another lexical translation will be. Thus, the input characters a--b symbolized (ยง3.5) as a , -- , b , which are not part of any grammatically correct program, even if the tokenization a , - , - >, b can be part of a grammatically correct program .
source share