Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator precedence in Java

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.

like image 317
ziggy Avatar asked Nov 13 '11 14:11

ziggy


People also ask

Which operator in Java has the highest precedence?

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.

What are the precedence of operators?

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.

What is precedence and associativity in Java?

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.


1 Answers

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

like image 127
Jon Skeet Avatar answered Oct 26 '22 17:10

Jon Skeet