An expression combines operands and operators. When an expression has several operators, fixed rules decide the order in which they are applied.

Arithmetic Expressions

An arithmetic expression combines variables, constants and arithmetic operators, such as a b − c / d. Mathematical formulas must be written in Java syntax: ax² + bx + c becomes a x x + b x + c, and (a + b) / (c − d) needs brackets. An expression is evaluated by computing each operator in turn, with the result stored by an assignment statement.

Definition: Operator precedence. The rule that fixes which operator in an expression is applied first when an expression contains more than one operator.

Precedence and Associativity

Precedence decides which operator is applied first. For example, has higher precedence than +, so 2 + 3 4 is 14, not 20. Associativity decides the order when operators have the same precedence. Most binary operators are left to right, so 20 − 5 − 3 is (20 − 5) − 3 = 12, and 20 / 5 2 is 8. The assignment and unary operators, and ?:, are right to left, so a = b = c = 0 assigns 0 to c first, then to b, then to a. Brackets* override the rules and also make an expression clearer.

Order of Precedence

From highest to lowest, the main levels are: postfix (x++, x−−); unary (++x, −−x, +, −, !, ~, casts); multiplicative (*, /, %); additive (+, −); shift (<<, >>, >>>); relational (<, >, <=, >=, instanceof); equality (==, !=); bitwise AND (&); bitwise XOR (^); bitwise OR (|); logical AND (&&); logical OR (||); conditional (?:); and assignment (=, +=, and so on).

Evaluation of Expressions

To evaluate an expression by hand, first evaluate brackets from the innermost, then apply operators in order of precedence, and among equal precedence, by associativity. Type conversion rules apply at each step. Operands are evaluated from left to right in Java, which matters when an operand has a side effect such as x++.

Table 2.4: Precedence and associativity (high to low)

Level Operators Associativity
Unary ++ −− ! ~ (type) Right to left
Multiplicative * / % Left to right
Additive + − Left to right
Relational < > <= >= Left to right
Equality == != Left to right
Logical AND, OR && then ∣∣ Left to right
Conditional ?: Right to left
Assignment = += −= Right to left

Example 2.4: Evaluating an expression

Problem: Evaluate x = 10 + 4 * 3 / 2 − 8 % 3 and y = 2 + 3 > 4 && 6 / 2 == 3.

Solution:

  1. For x: 4 * 3 = 12, then 12 / 2 = 6, and 8 % 3 = 2. Then 10 + 6 − 2 = 14.
  2. For y: 2 + 3 = 5 and 6 / 2 = 3. Then 5 > 4 is true and 3 == 3 is true.
  3. true && true is true.

Result: x = 14 and y = true.

public class Precedence {
    public static void main(String[] args) {
        int x = 10 + 4 * 3 / 2 - 8 % 3;
        boolean y = 2 + 3 > 4 && 6 / 2 == 3;
        int a, b, c;
        a = b = c = 7;                    // right to left
        System.out.println("x = " + x + ", y = " + y);
        System.out.println("a = " + a + ", b = " + b + ", c = " + c);
        System.out.println("20 - 5 - 3 = " + (20 - 5 - 3));
        double r = (5 + 3) / 2.0;
        System.out.println("(5 + 3) / 2.0 = " + r);
    }
}

Output

x = 14, y = true
a = 7, b = 7, c = 7
20 - 5 - 3 = 12
(5 + 3) / 2.0 = 4.0

Common mistakes

  • Assuming that operators are applied simply from left to right.
  • Writing a mathematical formula without the brackets it needs.
  • Forgetting that assignment is right-associative.

Key takeaways

  • Precedence decides which operator goes first.
  • Associativity decides the order for equal precedence.
  • Most binary operators are left to right; unary, ?: and assignment are right to left.
  • Use brackets for clarity.

Practice questions

  1. What is 2 + 3 * 4 in Java?
  2. What is the associativity of the assignment operator?
  3. What is 100 / 10 * 2?

Answers to the practice questions

  1. 14, since * has higher precedence than +.
  2. Right to left.
  3. 20, evaluated left to right.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II