Operator Precedence

Part of: Primitive Types

Order of Operations When an expression mixes operators, Java evaluates them in a fixed order called operator precedence : its version of "PEMDAS". Higher-precedence operators are applied first. From highest to lowest for AP CSA arithmetic: 1. Parentheses ( ) 2. Multiplicative : , /, % (same level) 3. Additive : +, - (same level) Associativity When operators share the same precedence, Java uses left-to-right associativity : it evaluates from left to right. Because /, , and % are equal precedence, 17 % 5 2 does the modulus first (leftmost), then multiplies. Parentheses Make Intent Clear Even when not strictly required, parentheses improve readability and prevent mistakes: Without the parentheses, a + b + c / 3.0 would divide only c by 3.0, a common bug. Tracing an Expression To evaluate by hand, repeatedly find the highest-precedence operator (leftmost on ties) and reduce: The answer is 12. The exam loves expressions like this, mixing , /, %, +, and -. Apply precedence and left-to-right associativity carefully, and use parentheses in your own code to remove all doubt.

Challenge: Evaluate the Expression