Java Basics: Operator Order

In Java there is an order to how operators are evaluated.

* (multiplication), / (division) and % (modulo) are evaluated first.

+ (addition) and – (subtraction) are evaluated next.

= (assignment) is evaluated last.

When there are multiple operators that would evaluated at the same time, they are picked in order from left to right.

For example:

answer = ab%c – d/e+f would be resolved in this order:

First, a * b would be evaluated.

Second, % c would be evaluated.

Third, d / c would be calculated.

Fourth, the + would be evaluated.

Fifth, the would be evaluated…

Finally the assignment (setting the variable answer to the calculated value) would be evaluated.

 

Leave a Reply

Your email address will not be published. Required fields are marked *