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.
We can remember the order using PEMDAS: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right).
Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want.
This is indeed the way that it works: exponentiation has higher precedence than multiplication and division, which in turn have higher precedence than addition and multiplication.
Almost everybody so far has confused order of evaluation with operator precedence. In Java the precedence rules make the expression equivalent to the following:
a + (b * c) / ( d - e )
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:
a
b
c
*
d
e
-
/
+
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:
public class Precedence
{
private static int a()
{
System.out.println("a");
return 1;
}
private static int b()
{
System.out.println("b");
return 2;
}
private static int c()
{
System.out.println("c");
return 3;
}
private static int d()
{
System.out.println("d");
return 4;
}
private static int e()
{
System.out.println("e");
return 5;
}
public static void main(String[] args)
{
int x = a() + b() * c() / (d() - e());
System.out.println(x);
}
}
Produces the output
a
b
c
d
e
-5
which clearly shows the multiplication is performed before the subtraction.
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:
int i = 2;
System.out.println(i * (i=3)); // prints '6'
int j = 2;
System.out.println((j=3) * j); // prints '9'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With