-2

While studying Java I came across the expression 4 + 20 / (3 - 1) * 2.

In Eclipse, this expression is evaluated to 24. So, it could be interpreted as 4 + ((20 / (3 - 1)) * 2).

Why is it wrong to interpret 4 + 20 / (3 - 1) * 2 as 4 + (20) / ((3 - 1) * 2) which would result to 9?

Thanks a lot for your help.

UPDATE

To be more specific in my question, mathematically, unless misunderstanding or misinterpretation on my part, 4 + 20 / (3 - 1) * 2 is ambiguous as I learn from this paper https://math.berkeley.edu/~gbergman/misc/numbers/ord_ops.html by George Bergman, a professor at the University of California, Berkeley. One can interpret it as 4 + ((20 / (3 - 1)) * 2) or 4 + (20) / ((3 - 1) * 2).

However, in Java, the 4 + ((20 / (3 - 1)) * 2) interpretation appears to be right while the 4 + (20) / ((3 - 1) * 2) interpretation appears to be wrong. In both interpretations, things in parentheses get computed first, then multiplication and division are performed and finally addition and subtraction are performed. The only difference that i see between the two is that in the right interpretation division is done first while in the wrong interpretation multiplication is done first.

My question is then in Java what is the key factor that makes one interpretation right and the other wrong.

Thanks!

MK777
  • 80
  • 2
  • 8
  • 2
    Why this question is posted in php – Utkarsh Dixit Feb 07 '15 at 02:26
  • Are you asking in general why the output is 24 or to which program language is this related? – Rizier123 Feb 07 '15 at 02:27
  • 2
    Two reasons: 1. left associativity; 2. operator precedence. All of these are defined in the JLS. – fge Feb 07 '15 at 02:27
  • This question has answer here. Please refer it. [http://stackoverflow.com/questions/15849990/need-clarification-on-bodmas-rule][1] [1]: http://stackoverflow.com/questions/15849990/need-clarification-on-bodmas-rule – Prudhvi Feb 07 '15 at 02:29
  • The evaluation in Java is very precisely specified in the Java Language Specification. – Thorbjørn Ravn Andersen Feb 07 '15 at 02:32
  • possible duplicate of [Why does (x += x += 1) evaluate differently in C and Javascript?](http://stackoverflow.com/questions/8979784/why-does-x-x-1-evaluate-differently-in-c-and-javascript) – l'L'l Feb 07 '15 at 02:35
  • Thanks for your feedback. @user3647254 The reason why I posted it in PHP is because I felt that precedence applies to every language. @ Rizier123 I want to know why 4 + 20 / (3 - 1) * 2 results to 24 and not 9. – MK777 Feb 07 '15 at 02:40
  • @MuhireKavuna This is because of the operator precedence and the associativity! First everything in the parentheses gets calculated after this * and / (from left to right) and at the end + and - (from left to right). (There are many answers, i think you can accept that one which you think is the best for you (http://meta.stackexchange.com/q/5234)) – Rizier123 Feb 07 '15 at 02:49

5 Answers5

2

Order of operations states that the inner most parentheses are evaluated first then multiplication and division from left to right then addition and subtraction left to right. The left to right principle is important here. This order of operations is standard in mathematics and Java (as well as most languages) follow the standard for mathematics. Order of Operations

J Blaz
  • 783
  • 1
  • 6
  • 26
  • 1
    Thanks a lot for bringing up the left to right principle. I think that is what I was missing. Knowing that multiplication and division have the same level of precedence I did not understand why in one interpretation division is done first and it is considered right while in the other multiplication is done first but it is considered wrong. – MK777 Feb 09 '15 at 01:42
  • No problem that is the easy one to miss. Ties go to the left. – J Blaz Feb 12 '15 at 03:28
1

Well, You evaluate the parenthesis first.

So its 4+20/2*2

then the division

So its 4+10*2

Then the multiplication

So its 4+20

Meaning the final answer is 24. This answer is independent of programming language and based on the order of operations or "PEMDAS"

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Ravioli27
  • 66
  • 2
1

There is nothing wrong with interpreting the way you describe. Once you start including parentheses you are effectively bypassing the usual order in which arithmetic operations are done. With no parentheses the usual order in which arithmetic operations are evaluated kick in. This is so that mathematicians can do maths in a consistent way and computations are done in a consistent way.

0

Java has something called 'precedence of operators' Please look at some of the examples here precedence of operators

However you can customize the order of operations in your program by using parenthesis appropriately.

Prudhvi
  • 2,276
  • 7
  • 34
  • 54
0

This is simple operator precedence:

4 + 20 / (3 - 1) * 2

First all parentheses get's calculated so, after this all * and / from left to right and at the end + and - from left to right:

1.    4 + 20 / (3 - 1) * 2
2.    4 + (20 / 2) * 2
3.    4 + (10 * 2)
4.    4 + 20
             = 24
Rizier123
  • 58,877
  • 16
  • 101
  • 156