System.out.print(3+2+1-5+4%2-1/4+6);
Output: 7
How does the sequence of performing math operators work? Does Java follow the algebraic PEMDAS or something else?
Yes, Java follows the standard arithmetic order of operations.
However, you may be expecting a different answer than what you got. This is because the value 1/4 is evaluated using integer arithmetic, because both the 1 and the 4 are integers. The result of 1/4 in integer arithmetic is 0.
To ask the compiler to evaluate this using floating point, use something like 1.0/4.0. After doing that, you should get the floating point result 6.75 (which is probably what you expected).
Java will follow an order of precedence(order of operations)
()*, /, % +, - That is all you'll need for this problem but there is more here.
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