6

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?

Edit

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
ziggy
  • 15,677
  • 67
  • 194
  • 287
  • 3
    I doubt `4 + 5 * 6 / 3` is being evaluated as `4 + (5 * (6 / 3))` It just happens to give the same result when evaluated properly (`4 + ( ( 5 * 6 ) / 3 )`). – Esailija Nov 13 '11 at 14:35

3 Answers3

12

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).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • See also [Java operator precedence guidelines](http://stackoverflow.com/questions/2137690/java-operator-precedence-guidelines). – trashgod Nov 13 '11 at 14:35
  • Thanks Jon. I do read the spec but the language used in it is not really for beginners so i struggle to understand it. Thanks. – ziggy Nov 13 '11 at 14:51
3

Are you sure?

4 + (5 * 6) / 3
4 + 30 / 3
4 + 10
14

4 + 5 * (6 / 3)
4 + 5 * 2
4 + 10
14

They produce the same output because adding the parentheses don't happen to change the result. For your other equation, the parentheses actually do change the result. By removing the parentheses in the equations I solved, the correct path to the result is the first one.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
2

The second one is wrong. See Jon Skeet's answer. Multiplicative operators evaluate left to right. The grouping for:

4 + 5 * 6 / 3

should be

4 + ((5 * 6) / 3).

In this case, though, the wrong grouping

4 + (5 * (6 / 3))

yields the same answer.

Community
  • 1
  • 1
Richard Povinelli
  • 1,419
  • 1
  • 14
  • 28