Java. Arithmetic operations. How?

Who can explain or give a good link to understand this example:

int a=1; int b=2; System.out.println(a---b); //correct System.out.println(a- -b); //correct System.out.println(a--b); //wrong 

thanks.

+4
source share
3 answers

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); // prints 0 System.out.println(a); // prints -1 

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 .

+4
source

- and -- are unary operators. Therefore, it cannot be used with two operands. That's why

 System.out.println(a--b); 

wrong. -- applies to a , so the new value of a is 0 . If you add another - , then the value of a reduced by 1 will be subtracted by the value of b , giving -2

0
source

All Articles