Expressions, Operators, Precedence and Assignment (ISC Class 12 Computer Science)
Updated 17 October 2026 · Draft, not yet fact-checked
A Java expression is evaluated by operator precedence (higher first) and then associativity (left to right for most operators, right to left for assignment). Highest to lowest: postfix and prefix ++ --, unary, * / %, + -, relational, == !=, &&, ||, ternary, assignment. A final variable is a named constant that cannot be reassigned.
Operators higher on the ladder are evaluated first; the arrow in each row shows the direction for ties, and unary, ternary and assignment go right to left.
Illustration drawn for QuestionBuddy.
Understand Expressions, Operators, Precedence and Assignment in Java
Variables and constants. A variable is a name for a memory location holding a value of a declared type. A named constant is declared final, for example final double PI = 3.14;; assigning to it again is a compile-time error.
Assignment. In x = x + 1; the right-hand side (RHS) is evaluated to a value first; the left-hand side (LHS) names the variable that receives it. The LHS must be a variable, so 5 = x; and x + 1 = y; are invalid. Assignment is right-associative: a = b = 4; sets b then a. Compound forms +=, -=, *=, /=, %= include a cast back to the type of the LHS, and the LHS value is read before the RHS is evaluated.
Operators.
- Arithmetic:
+ - * / %./between ints is integer division;%gives the remainder and takes the sign of the left operand (-17 % 5 is -2). - Increment and decrement:
i++(use the value, then add 1) and++i(add 1, then use the value). - Relational:
< <= > >=; equality== !=; result is boolean. - Logical:
!,&&,||.&&and||are short-circuit: the right side is skipped when the left side already decides the result. The non-short-circuit forms&and|always evaluate both sides. - Ternary:
cond ? x : y.
Precedence from highest to lowest: postfix ++ --; prefix ++ -- and unary + - !; * / %; + -; < <= > >=; == !=; &&; ||; ?:; assignment operators. Operators at the same level are evaluated left to right, except unary, ternary and assignment which go right to left.
String concatenation. + with a String operand joins text. Evaluation is left to right: 1 + 2 + "A" + 1 + 2 is "3A12".
Operand types and casting are in the previous topic; the if, switch and loop statements are in the next chapter.
Expressions, Operators, Precedence and Assignment in Java: key facts
- Precedence order
- Postfix ++ --
- Prefix ++ -- ! unary
- multiply divide remainder
- + -
- < <= > >=
- == !=
- &&
- ||
- ?:
- assignment
- Highest first; same level goes left to right.
- Increment forms
- a++ uses the old value, then adds 1
- ++a adds 1, then uses the new value
- Same for decrement
- Trace each use separately.
- Remainder and division
- (a / b) times b + a % b = a for ints
- The sign of a % b follows a
- -17 % 5 = -2
- 17 / 5 = 3
- Integer division drops the fraction.
- Short-circuit
- false && x skips x
- true || x skips x
- & and
- always evaluate both sides
- Used to guard a division by zero.
How to answer Expressions, Operators, Precedence and Assignment in Java questions
Use this routine to evaluate an expression or trace a program.
- 1Write the current value of every variable in a small table.
- 2Fully bracket the expression using precedence and associativity.
- 3Evaluate the innermost bracket first; for a++ and ++a update the variable at the right moment.
- 4Apply integer division when both operands are int.
- 5For && and ||, stop as soon as the result is decided and do not execute the remaining side effects.
- 6Update the table and write the printed output exactly.
Expressions, Operators, Precedence and Assignment in Java in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Billing with a fixed surcharge
Suppose a shop program defines final double SURCHARGE = 0.25; and charges 500.0 on an amount.
What is the surcharge and the total, and what happens if the program later tries SURCHARGE = 0.5;?
Try it first, then show the solutionSolution
- 500.0 x 0.25 = 125.0, so total = 625.0.
- A final variable cannot be reassigned: compile-time error.
Answer: Surcharge 125.0, total 625.0; the reassignment fails to compile.
Memory hook: final fixes the value for ever.
Guarding a division
Suppose a program computes total / count only when count is not zero, written as (count != 0) && (total / count > 10).
What happens when count is 0?
Try it first, then show the solutionSolution
- count != 0 is false.
- && short-circuits, so the division is never evaluated.
Answer: The result is false and no exception occurs.
Memory hook: The guard goes first, on the left.
Tricks to remember Expressions, Operators, Precedence and Assignment in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
MD before AS
Multiply, divide, remainder before add, subtract; brackets beat all.
Use it when: Arithmetic precedence.
a++ after, ++a before
The sign after the name acts after use; the sign before acts first.
Use it when: Increment tracing.
AND stops on false, OR stops on true
False && anything is false; true || anything is true.
Use it when: Short-circuit questions.
LHS holds, RHS computes
The right side makes a value; the left side is the place it goes.
Use it when: Assignment questions.
Expressions, Operators, Precedence and Assignment in Java: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Simple precedence · 1 mark · MCQBoard level
The value of 10 + 2 * 3 in Java is
(a) 36 (b) 16 (c) 18 (d) 60
Try it first, then show the solutionSolution
- * has higher precedence than +, so 2 * 3 = 6 first.
- 10 + 6 = 16.
Answer: (b) 16
Example 2: Prefix and postfix together · 3 marks · Short answerHarder board level
Write the values of r, a and b:
int a = 5, b = 3;
int r = a++ + ++b * 2;
System.out.println(r + " " + a + " " + b);Try it first, then show the solutionSolution
- a++ gives 5 and then a becomes 6. (1 mark)
- ++b makes b 4 and gives 4; 4 * 2 = 8. (1 mark)
- r = 5 + 8 = 13. Output: 13 6 4. (1 mark)
Answer: 13 6 4
Example 3: Compound assignment with increments · 3 marks · Short answerHarder board level
Find the value of x after:
int x = 10;
x += x++ + ++x;
System.out.println(x);Try it first, then show the solutionSolution
- x += e means x = x + e, and the old x (10) is read first. (1 mark)
- x++ gives 10 and x becomes 11; ++x makes x 12 and gives 12; the sum is 22. (1 mark)
- x = 10 + 22 = 32. (1 mark)
Answer: 32
Example 4: Short-circuit versus non-short-circuit · 4 marks · Short answerHarder board level
Write the output:
int a = 4, b = 0;
boolean r1 = (b != 0) && (a / b > 1);
boolean r2 = (b == 0) || (a / b > 1);
boolean r3 = (a > 3) & (b++ == 0);
System.out.println(r1 + " " + r2 + " " + r3 + " " + b);Try it first, then show the solutionSolution
- r1: b != 0 is false, so && skips a / b: no ArithmeticException, r1 = false. (1 mark)
- r2: b == 0 is true, so || skips the division, r2 = true. (1 mark)
- r3: & evaluates both sides. a > 3 is true; b++ == 0 compares the old b (0), so true, and b becomes 1. (1.5 marks)
- Output: false true true 1. (0.5 mark)
Answer: false true true 1
Example 5: Case: billing with a constant · 4 marks · Case-basedHOTS / competency
Suppose a program contains:
final double RATE = 0.25;
double amt = 500;
double tax = amt * RATE;
System.out.println(tax + " " + (amt + tax));
System.out.println(1 + 2 + "A" + 1 + 2);(i) Write the output of both statements. (2 marks)
(ii) What error arises if the line RATE = 0.5; is added? (1 mark)
(iii) Why is the second output not 12A12? (1 mark)
Try it first, then show the solutionSolution
- (i) tax = 500 x 0.25 = 125.0 and amt + tax = 625.0, so the first line is 125.0 625.0. The second line is 3A12.
- (ii) A compile-time error: a final variable cannot be assigned again.
- (iii) Evaluation is left to right: 1 + 2 is added first as numbers (3), then joined with "A", then the 1 and 2 are appended as text.
Answer: (i) 125.0 625.0, then 3A12 (ii) cannot assign a value to a final variable (iii) left-to-right evaluation adds 1 + 2 before the String appears
Where students lose marks in Expressions, Operators, Precedence and Assignment in Java
The slips examiners see most often, and what to write instead.
Mistake: Treating a++ as ++a
The old value is used in the expression when the sign follows the name.
Wrong working
a = 5; r = a++ + 1 gives 7
Right working
a = 5; r = a++ + 1 gives 6 and a becomes 6
Do this: Use the value, then update.
Usually costs: 1 mark
Mistake: Left-to-right surprise with strings
Numbers before the first String are added, not joined.
Wrong working
1 + 2 + "A" gives 12A
Right working
1 + 2 + "A" gives 3A
Do this: Evaluate from the left.
Usually costs: 1 mark
Mistake: Forgetting that a final variable is fixed
Any later assignment is a compile-time error.
Wrong working
final int N = 5; N = 6;
Right working
final int N = 5; int m = N + 1;
Do this: Write the value once at declaration.
Usually costs: 1 mark
Exam tips
- Write a small variable table for any increment question and update it step by step.
- Show the bracketed form of the expression before evaluating it.
- Mention integer division whenever both operands are int.
- Use final and capital letters for a named constant when the question says "constant".
- Print doubles with a decimal point (125.0) in the output.
Practice questions on Expressions, Operators, Precedence and Assignment in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
What is the value of -17 % 5 in Java?
(a) 2 (b) -2 (c) 3 (d) -3Hint
The sign follows the left operand.
Answer
(b) -2
Q2 · 2 marks · Short answerBoard level
Explain the difference between the left-hand side and the right-hand side of an assignment, and say why 5 = x; is invalid.
Hint
One is a place, the other a value.
Answer
In an assignment the right-hand side is an expression evaluated to a value; the left-hand side must be a variable (a storage location) that receives it. 5 = x; is invalid because 5 is a constant value, not a variable that can store something.
Q3 · 3 marks · Short answerHarder board level
Write the output:
int p = 7, q = 2; double r = p / q * 2.0 + p % q; p -= q * 2 + 1; System.out.println(r + " " + p);Hint
p / q is integer division.
Answer
p / q = 3 (integer), 3 * 2.0 = 6.0, p % q = 1, so r = 7.0. Then p = 7 - (2 * 2 + 1) = 2. Output: 7.0 2
Q4 · 3 marks · Short answerHarder board level
Write the output:
int a = 8, b = 3; int m = a > b ? a - b : b - a; boolean t = a % 2 == 0 && b % 2 != 0 || a < b; int z = a - b - 2; System.out.println(m + " " + t + " " + z);Hint
Bracket by precedence: && before ||.
Answer
a > b is true, so m = 8 - 3 = 5. t = ((true) && (true)) || false = true. z = (8 - 3) - 2 = 3. Output: 5 true 3
Q5 · 4 marks · Case-basedHOTS / competency
A student swaps a and b without a third variable: a = a + b; b = a - b; a = a - b;. (i) Trace it for a = 6, b = 9. (ii) Trace it for a = 2000000000, b = 1000000000, where a + b overflows an int, and say whether the swap still works.
Hint
int arithmetic wraps by 4294967296.
Answer
(i) a = 15; b = 15 - 9 = 6; a = 15 - 6 = 9. Swapped. (ii) a + b = 3000000000, which wraps to -1294967296. b = -1294967296 - 1000000000 = -2294967296, which wraps to 2000000000. a = -1294967296 - 2000000000 = -3294967296, which wraps to 1000000000. The swap still works because int arithmetic wraps consistently, but the intermediate overflow is a risk and a temporary variable is safer.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Which has higher precedence, && or ||?Show answer
&&.
2.What is the value of 17 / 5 and 17 % 5?Show answer
3 and 2.
3.What does final do to a variable?Show answer
Makes it a constant that cannot be reassigned.
4.What does ++a do differently from a++?Show answer
It increments first and then uses the new value.
5.What is a short-circuit operator?Show answer
&& or ||, which skip the right side when the left side decides the result.
6.Which side of an assignment is evaluated first?Show answer
The right-hand side value is computed, then stored in the left-hand variable.
Expressions, Operators, Precedence and Assignment in Java: frequently asked questions
What is the order of precedence of operators in Java?
Postfix and prefix increment, unary, multiplicative, additive, relational, equality, logical AND, logical OR, ternary and then assignment, from highest to lowest.
What is the difference between == and = in Java?
= assigns the right-hand value to the variable on the left; == compares two values and gives a boolean.
What is the difference between & and && in Java?
&& short-circuits and skips the right side when the left side decides the result; & always evaluates both sides.
How do you declare a constant in Java?
Use the keyword final, for example final int MAX = 100;. The name is usually written in capital letters.