Precedence of arithmetic operators in Java

1.9k Views Asked by At

I was reading "Programming in Java Deitel & Deitel 7th Edition" and I watched the precedence of operators but I was with some trepidation.

The author says that when there is a mathematical typical expression in which there are operators of equal precedence operation runs from left to right. For example:

System.out.println (a + b + c - d);

In this sense the operation is executed from left to right, then I research on the internet and I find that the "=" operator runs from right to left. For example:

int x;
x = 4 * 4 * 4% 2;

All taxes fine until I ponogo to investigate further and write code and it occurs to me this, I wrote this exeption:

System.out.println ((4 * 4) + (8 * 8) * (4 * 4) - 16/4);

And my question is this, if the book says that the addition and subtraction have lower priority compared to other operators, What if I add the value of a typical expression multiplied ?. Let me explain:

When I put (4 * 4) + (8 * 8) * (4 * 4) I am adding the value of the multiplication, then lower the precedence of the operator?

Another question:

If I have an example as follows:

System.out.println (a * b * c + d + e + f * g / h);

As higher precedence operations are executed first order from left to right, when I get the time to run the higher precedence Does it run from left to right as well?

When the value of a variable is assigned, Do operators of higher precedence associate from right to left?

2

There are 2 best solutions below

0
On

Usually, the code is always read from left to right, but also follows the math precedence. Example:

(4 * 4) + (8 * 8) * (4 * 4) - 16/4

Math says in this case: Parentheses first, multiplication and division after, and then addition and subtraction. Then:

//Parenthesis done
(16) + (64) * (16) - 16/4

//Multiplication and division done
16 + 1024 - 4

//Addition and subtraction done
1036
0
On

I'll try to answer this with some references, so that you can go into whatever depth you want. It's not entirely clear what is confusing you.

Firstly, you need to be clear on the difference between assignment operators (= in your examples, but these also include e.g. += etc) and other (binary) operators (e.g. +,-,*,% etc). Assignment operators (such as =) evaluate right to left - i.e. they work out the value of everything on the right of the operator and then assign it to whatever is on the left.

For other operators, they first apply operator precedence and then apply left to right operation on operators with equal precedence. A table of precedence and discussion of this is available in the official tutorial. It is very similar to the table (Fig A.1) on p.1509 of the book you are using, but the web tutorial explanation may be easier to understand. It can be summed up as precedence being applied as expressions in brackets first, followed by postfix then unary operators (e.g. x++, then -x), then multiplicative operators (*,\,%) and then additive (+,-) and finally lots of logical operators that are not directly relevant to this question.

Finally, the tutorial contains the statement

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Crucially, the precedence then left-to-right evaluation will be repeated, most easily illustrated by going through your example step by step:

(4 * 4) + (8 * 8) * (4 * 4) - 16/4
// Found brackets - highest precedence.
// more than one, so evaluate left to right
16 + (8 * 8) * (4 * 4) - 16/4
16 + 64 * (4 * 4) - 16/4
16 + 64 * 16 - 16/4
//brackets finished, back to precedence.  Found multiplicative operators
//evaluate left to right
16 + 1024 - 16/4
16 + 1024 - 4
//multiplicative finished, back to precedence.  Found additive operators
//evaluate left to right
1040 - 4
1036
// Expression fully evaluated. Can now be used / assigned to a variable

Addendum

The definitive specification for the evaluation of expressions is in the Java Language Specification chapter 15. It's probably more confusing than helpful at this level, but I include it for completeness. Section 15.7.3 deals with respecting brackets (parantheses) around expressions and operator precedence.