Java Variables, Arithmetic Operators and Precedence (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
A variable is a named memory location with a type, declared as type name; and given a value by assignment; a named constant is declared with final and cannot change. Arithmetic operators are + - * / %; ++ and -- increase or decrease by 1 (prefix changes first, postfix uses the old value first). In an expression, operators of higher precedence are evaluated first, and operators of equal precedence go left to right, except assignment, which goes right to left.
Work from the top rung down: * and % before + and -, equal rungs go left to right, and only assignment goes right to left.
Illustration drawn for QuestionBuddy.
Understand Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence
Variables. Declaration: int count; double price = 49.5; char grade = 'A'; Names (identifiers): letters, digits, _ and $, not starting with a digit, not a keyword, case-sensitive. A local variable must be given a value before use. In an assignment the left side (l-value) is a variable and the right side (r-value) is an expression; a = b = c = 5 works because assignment goes right to left.
Named constants. final double PI = 3.14159; the value cannot be changed later; reassigning PI is a compile-time error. By convention constants are written in capitals.
Arithmetic operators. + - * / % (remainder: 17 % 5 = 2; -17 % 5 = -2 in Java, the sign follows the dividend). int / int is integer division: 17 / 5 = 3.
Increment and decrement. x++ (postfix): use the value, then add 1. ++x (prefix): add 1, then use. int a = 5; int b = a++ + ++a; gives a = 7 and b = 5 + 7 = 12. Compound assignment: x += y means x = x + y (also -=, *=, /=, %=).
Precedence (high to low). ( ), ++ -- (postfix), prefix ++ -- and unary + - !, * / %, + -, relational, equality, &&, ||, ?:, assignment. Associativity: left to right for binary operators; right to left for assignment, unary operators and ?:.
10 + 5 * 3 % 4 - 2: 5 * 3 = 15; 15 % 4 = 3; 10 + 3 - 2 = 11.
Mixed types. Integer operands give integer results: 17 / 5 * 2 = 6 but 17 % 5 * 2 = 4; with a double operand the result is double: 17 / (5 * 2.0) = 1.7.
Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence: key facts
- Increment forms
- x++ uses then adds 1
- ++x adds 1 then uses
- x += y means x = x + y
- Side effects.
- Precedence order
- Brackets, unary and increment
- multiply, divide, remainder
- add, subtract
- relational
- &&
- ||
- ?:
- assignment
- High to low.
- Division
- int / int = int | % gives the remainder with the sign of the dividend
- 17 / 5 = 3; 17 % 5 = 2.
How to answer Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence questions
Evaluate step by step following precedence, and update variables in order.
- 1Bracket the expression by precedence and associativity.
- 2Evaluate postfix and prefix operations left to right, recording the variable after each.
- 3Apply integer division where both operands are int.
- 4Write the final values of all variables.
Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence 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.
Constants
Suppose a billing program uses a tax rate of 18% in 25 places and the rate later changes.
Why declare it as a named constant?
Try it first, then show the solutionSolution
- final double TAX = 0.18; is declared once.
- Changing the single declaration updates all calculations and prevents accidental changes.
Answer: One place to change and protection against accidental changes.
Memory hook: final means final.
Tricks to remember Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence
Short memory aids. Say each one aloud once, then use it on a question the same day.
Prefix first, postfix later
++x changes before use; x++ after.
Use it when: Increment.
Times, divide, mod same level
* / % share precedence, left to right.
Use it when: Precedence.
Assignment from the right
a = b = 5 assigns b first.
Use it when: Associativity.
Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Postfix · 1 mark · MCQBoard level
After int x = 5; int y = x++; the values of x and y are:
(a) 5, 5 (b) 6, 5 (c) 6, 6 (d) 5, 6
Try it first, then show the solutionSolution
- y gets the old value 5; then x becomes 6.
Answer: (b) 6, 5
Example 2: Evaluate · 5 marks · Short answerBoard level
Evaluate: (i) 10 + 5 * 3 % 4 - 2 (ii) 17 / 5 * 2 (iii) 17 % 5 * 2 (iv) 17 / (5 * 2.0) (v) -17 % 5. (5 marks)
Try it first, then show the solutionSolution
- (i) 5 * 3 = 15; 15 % 4 = 3; 10 + 3 - 2 = 11.
- (ii) 17 / 5 = 3; 3 * 2 = 6.
- (iii) 17 % 5 = 2; 2 * 2 = 4.
- (iv) 5 * 2.0 = 10.0; 17 / 10.0 = 1.7.
- (v) -17 % 5 = -2 (sign of the dividend).
Answer: (i) 11; (ii) 6; (iii) 4; (iv) 1.7; (v) -2
Example 3: Increment and decrement · 6 marks · Long answerHarder board level
Find the values of a and b after each: (a) int a = 5; int b = a++ + ++a; (b) int a = 4; int b = a-- - --a; (c) int x = 10; x += x++ + ++x; (6 marks)
Try it first, then show the solutionSolution
- (a) a++ gives 5 (a becomes 6); ++a makes a 7 and gives 7: b = 5 + 7 = 12, a = 7.
- (b) a-- gives 4 (a becomes 3); --a makes a 2 and gives 2: b = 4 - 2 = 2, a = 2.
- (c) The left x is read as 10 first; x++ gives 10 (x = 11); ++x makes x 12 and gives 12; the sum is 22; x = 10 + 22 = 32.
Answer: (a) a = 7, b = 12; (b) a = 2, b = 2; (c) x = 32
Example 4: Program: swap and digit · 4 marks · Short answerHarder board level
Write the output: int a = 7, b = 3; a = a + b; b = a - b; a = a - b; System.out.println(a + " " + b); int n = 482; int d = n % 10; n = n / 10; System.out.println(d + " " + n); Also state the effect of int c = a += 2; (4 marks)
Try it first, then show the solutionSolution
- a = 10; b = 10 - 3 = 7; a = 10 - 7 = 3: output 3 7.
- d = 2; n = 48: output 2 48.
- a += 2 makes a = 5, and c gets the value of the assignment: c = 5.
Answer: 3 7
2 48
c = 5 and a = 5 (assignment is an expression with a value).
Example 5: Constant and compile error · 5 marks · Long answerHOTS / competency
Identify the errors in: final int MAX = 10; MAX = 20; int 2a = 5; int x; System.out.println(x); double d = 7 / 2; and state the value of d. Correct each. (5 marks)
Try it first, then show the solutionSolution
- MAX = 20: cannot assign to a final variable.
- int 2a: identifier cannot start with a digit; use a2.
- System.out.println(x): variable x might not have been initialised; assign a value first.
- double d = 7 / 2: integer division gives 3, stored as 3.0; to get 3.5 write 7 / 2.0.
Answer: Errors: reassigning a final constant, an identifier starting with a digit, use of an uninitialised local variable; d is 3.0 (use 7 / 2.0 for 3.5).
Where students lose marks in Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence
The slips examiners see most often, and what to write instead.
Mistake: Confusing prefix and postfix
Order error.
Wrong working
a++ + ++a = 5 + 6 = 11
Right working
5 + 7 = 12
Do this: Update a after the first use.
Usually costs: 2 marks.
Mistake: Ignoring integer division
Maths habit.
Wrong working
17 / 5 * 2 = 6.8
Right working
6
Do this: int / int is int.
Usually costs: 2 marks.
Mistake: Left to right for assignment
Habit.
Wrong working
a = b = 5 assigns a first
Right working
b first
Do this: Assignment is right-associative.
Usually costs: 1 mark.
Exam tips
- Write the value of each variable after every operator in increment questions.
- Bracket the expression before evaluating.
- Remember % keeps the sign of the dividend.
Practice questions on Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence
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
Which of these is a valid identifier in Java?
(a) 1rate (b) rate_1 (c) class (d) my-rateHint
Not starting with a digit, no hyphen, not a keyword.
Answer
(b) rate_1
Q2 · 2 marks · Short answerBoard level
State the output: int p = 9, q = 4; System.out.println(p / q + " " + p % q + " " + (p + q * 2) + " " + (p + q) * 2); (2 marks)
Hint
Precedence.
Answer
2 1 17 26
Q3 · 4 marks · Short answerHarder board level
Find the values of x and y after: int x = 3; int y = x++ * 2 + --x; then int z = y++ + y; give z and the final y. (4 marks)
Hint
x++ gives 3, x becomes 4; --x gives 3.
Answer
x++ gives 3 (x = 4); x++ * 2 = 6; --x makes x 3 and gives 3: y = 9, x = 3. Then y++ gives 9 (y = 10); z = 9 + 10 = 19; final y = 10.
Q4 · 4 marks · Short answerHarder board level
Evaluate the expression: 8 + 6 / 4 * 2 - 7 % 3 + (5 - 2) * 3, and then rewrite it with doubles (6 / 4.0) to see the difference. (4 marks)
Hint
6 / 4 = 1.
Answer
6 / 4 = 1; 1 * 2 = 2; 7 % 3 = 1; (5 - 2) * 3 = 9: 8 + 2 - 1 + 9 = 18. With 6 / 4.0 = 1.5; 1.5 * 2 = 3.0: 8 + 3.0 - 1 + 9 = 19.0.
Q5 · 3 marks · Short answerHOTS / competency
A programmer writes int total = 0; total = total++ + 5; and expects 6, but gets 5. Trace the statement and explain. (3 marks)
Hint
Postfix assigns the old value.
Answer
total++ gives the old value 0 and sets total to 1, then 0 + 5 = 5 is assigned to total, overwriting the 1. So total is 5. To get 6 write total = ++total + 5 (gives 1 + 5 = 6) or total += 6.
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.Keyword for a constant?Show answer
final.
2.17 / 5 in Java?Show answer
3.
3.x++ means?Show answer
Use then increment.
4.Assignment associativity?Show answer
Right to left.
5.-17 % 5?Show answer
-2.
Variables, Constants, Arithmetic Operators, Increment and Decrement, and Precedence: frequently asked questions
Can an identifier have a hyphen?
No.
What is a keyword?
A reserved word like class.
Are constants usually capitals?
Yes, by convention.
What does %= do?
Takes the remainder and assigns.