An operator performs an operation on values (operands) to produce a result. An expression combines values and operators (like a + b * 2). This chapter covers Java's operators (ICSE Class 9).
1Forms & types of operators
By the number of operands they take:
| Form | Operands | Example |
|---|---|---|
| Unary | 1 | -a, ++a, !flag |
| Binary | 2 | a + b, a > b |
| Ternary | 3 | (a > b) ? a : b |
Types of operators
| Type | Operators |
|---|---|
| Arithmetic | + - * / % |
| Relational | < > <= >= == != |
| Logical | && (and) , || (or) , ! (not) |
| Assignment | = |
| Shorthand | += -= *= /= %= |
condition ? valueIfTrue : valueIfFalse is a compact if-else: int big = (a > b) ? a : b; stores the larger of a and b.- By operands: unary (1), binary (2), ternary (3).
- Types: arithmetic (+ - * / %), relational (< > == !=), logical (&& || !), assignment (=), shorthand (+= etc.).
- The ternary operator condition ? a : b is a short if-else.
2Increment/decrement: prefix vs postfix
++ adds 1 and -- subtracts 1. Where you put it matters:
- Prefix (
++a) — increment FIRST, then use the new value. - Postfix (
a++) — use the OLD value first, then increment.
int a = 5; int x = ++a; // a becomes 6, then x = 6 (prefix) int b = 5; int y = b++; // y = 5 (old value), then b becomes 6 (postfix)
- ++ adds 1, -- subtracts 1.
- Prefix (++a) increments first, then uses the new value.
- Postfix (a++) uses the old value first, then increments.
3Precedence, associativity & special operators
When an expression has several operators, precedence decides which runs first (like BODMAS in maths):
int r = 2 + 3 * 4; // * before + , so 2 + 12 = 14 int s = (2 + 3) * 4; // brackets first, so 5 * 4 = 20
Rough order (high → low): () → ++ -- → * / % → + - → relational → logical → assignment. Associativity decides ties (most operators go left-to-right).
Two special operators
- new — creates an object (allocates memory):
Student s = new Student(); - dot (.) — accesses a member of an object:
s.display(),Math.sqrt(9)
- Precedence sets which operator runs first (*, / before +, -); brackets () override it.
- Associativity breaks ties (most operators are left-to-right).
- new creates an object (allocates memory); the dot (.) accesses an object's member.
★ Practical: operators in code
In BlueJ, write code that:
- Reads two numbers and prints their sum, difference, product and remainder.
- Uses the ternary operator to print the larger of the two.
- Shows the difference between ++a and a++ by printing the results.
- Evaluates 2 + 3 * 4 and (2 + 3) * 4 and explains why they differ.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.