System.out.println(100% - 40);
Please explain the steps the compiler takes to resolve this.
Like I understand % is an operator which takes two operands to work but how is it accepting another operator like "-" minus in this case?
The formatting here is important to read it properly.
System.out.println(100 % -40);
or
System.out.println(100 % (-40));
Then, it becomes clear that there are two operators: the remainder operator %
and the unary minus operator -
.
Furthermore, the operator precedence table explains why this terribly unusual expression works even without round brackets.
┌────────────────┬───────────────────────────────┐
│ Operators │ Precedence ↓ │
├────────────────┼───────────────────────────────┤
│ unary │ ++expr --expr +expr -expr ~ ! │
│ multiplicative │ * / % │
└────────────────┴───────────────────────────────┘