What is the correct sequence of the math operations in this expression in Java:
a + b * c / ( d - e )
1. 4 1 3 2
2. 4 2 3 1
I understand that result is the same in both answers. But I would like to fully understand the java compiler logic. What is executed first in this example - multiplication or the expression in parentheses? A link to the documentation that covers that would be helpful.
UPDATE: Thank you guys for the answers. Most of you write that the expression in parentheses is evaluated first. After looking at the references provided by Grodriguez I created little tests:
int i = 2;
System.out.println(i * (i=3)); // prints '6'
int j = 2;
System.out.println((j=3) * j); // prints '9'
Could anybody explain why these tests produce different results? If the expression in parentheses is evaluated the first I would expect the same result - 9.
i am assuming that your expression would be something like
x = a + b * c / ( d - e )
the equality operator has right to left order of evaluation. so the expression on the right of = will be evaluated first.
if your refer this precedence chart: http://www.java-tips.org/java-se-tips/java.lang/what-is-java-operator-precedence.html
1) the brackets will be evaluated (d-e), lets say (d - e) = f so the expression then becomes x = a + b * c / f.
2) Now * and / have same precedence, but the order of evaluation is left to right to * will be evaluated first so lets say b * c = g, so the expression becomes x = a + g /f
3) Now / has the next precedence so g / f will be evaluated to lets say h so the expression will be come x = a + h,
4) lastly evaluating a + h
The results of the calculation are defined by the Operator Order of Precedence. So parentheses have highest precedence here, multiplication and division next highest, and addition and subtraction lowest. Operators with equal precedence are evaluated left to right. So the expression in the question is equivalent to:
However there is a slight difference between what is normally meant by "being evaluated first" and the operator precedence for getting the correct answer.
"d-e" is not necessarily actually calculated before "a" is calculated. This pretty much doesn't make any difference unless one of the 'variables' in the expression is actually a function. The Java standard does not specify the order of evaluation of components of an expression.
As JeremyP has nicely shown us, the first answer is correct.
In general, the following rules apply:
||
,&&
, and?
:
)Note that the first two rules explain the result in your second question:
Reference documentation:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779
Tutorial:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
In your second question, it seems Java is evaluating the part in parenthesis as an assignment, not an mathematical expression. This means that is will not perform parenthetical assignments in the same order as operations in parenthesis.
It evaluates the expressions in the following order. Variable names are expressions that need to be evaluated.
So, the answer to your question is #1. The order of operations determines the shape of the expression tree (what is the left side of the tree, and what is the right), but the left side is always evaluated first (and the root is evaluated last).
Almost everybody so far has confused order of evaluation with operator precedence. In Java the precedence rules make the expression equivalent to the following:
because
*
and/
have equal precedence and are left associative.The order of evaluation is strictly defined as left hand operand first, then right, then operation (except for || and &&). So the order of evaluation is:
order of evaluation goes down the page. Indentation reflects the structure of the syntax tree
Edit
In response to Grodriguez's comments. The following program:
Produces the output
which clearly shows the multiplication is performed before the subtraction.