In one example from http://leepoint.net/notes-java/data/expressions/precedence.html
The following expression
1 + 2 - 3 * 4 / 5
Is evaluated as
1 + 2 - 3 * 4 / 5
= (1 + 2) - ((3 * 4) / 5)
= 3 - (12/5)
= 3 - 2 The result of the integer division, 12/5, is 2 .
= 1
Then i saw another example from http://www.roseindia.net/java/master-java/operator-precedence.shtml
The following expression
4 + 5 * 6 / 3
is evaluated as
4 + (5 * (6 / 3))
I am slightly confused as to how it is decided which will be evaluated first when * and / are involved. In the examples above, both seem to be difference.
The first example is evaluating 3*5/5
as ((3*4)/5)
Whereas the second example is evaluating 5*6/3 as (5*(6/3))
I know that * and / have precedence over + and - but what about when the expression includes both * and /. And also why are the above two examples showing different approaches? Is one of them wrong?
public class ZiggyTest {
public static void main(String[] args) {
System.out.println(4 + (5 * (6 / 3)));
System.out.println(4 + ((5 * 6) / 3));
System.out.println(1 + 2 - (3 * (4 / 5)));
System.out.println(1 + 2 - ((3 * 4) / 5));
}
}
The above program produces the output
14
14
3
1
Why are the last two outputs not the same if the first produced the same output.
In Java, parentheses() and Array subscript[] have the highest precedence in Java. For example, Addition and Subtraction have higher precedence than the Left shift and Right shift operators. Below is a table defined in which the lowest precedence operator show at the top of it.
The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3 , the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary.
The operator's precedence refers to the order in which operators are evaluated within an expression whereas associativity refers to the order in which the consecutive operators within the same group are carried out. Precedence rules specify the priority (which operators will be evaluated first) of operators.
I am slightly confused as to how it is decided which will be evaluated first when * and / are involved
That's why we have specifications :)
Section 15.7 is the section of the Java Language Specification which deals with evaluation order, and section 15.17 states:
The operators *, /, and % are called the multiplicative operators. They have the same precedence and are syntactically left-associative (they group left-to-right).
So whenever there is A op1 B op2 C
and both op1
and op2
are *
, /
or %
it's equivalent to
(A op1 B) op2 C
Or to put it another way - the second linked article is plain wrong in their example. Here's an example to prove it:
int x = Integer.MAX_VALUE / 2;
System.out.println(x * 3 / 3);
System.out.println((x * 3) / 3);
System.out.println(x * (3 / 3));
Output:
-357913942
-357913942
1073741823
That shows the multiplication happening first (leading to integer overflow) rather than the division (which would end up with a multiplication of 1).
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