Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right precedence of the math expression

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.

like image 350
bancer Avatar asked Oct 26 '10 12:10

bancer


People also ask

What is the order of precedence in math?

We can remember the order using PEMDAS: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right).

Which has more precedence * or?

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.

Which has highest precedence in expression?

Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want.

Which has higher precedence power or multiplication?

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.


2 Answers

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.

like image 139
JeremyP Avatar answered Oct 11 '22 23:10

JeremyP


As JeremyP has nicely shown us, the first answer is correct.

In general, the following rules apply:

  • Every operand of an operator is evaluated before the operation itself is performed (except for ||, &&, and ? :)
  • Operands are evaluated left to right. The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
  • The order of evaluation respects parentheses and operator precedence:
    • Parentheses are evaluated first.
    • Operators are evaluated in order of precedence.
    • Operators with equal precedence are evaluated left-to-right, except for assignment operators which are evaluated right-to-left.

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

like image 31
Grodriguez Avatar answered Oct 11 '22 21:10

Grodriguez