➗ Object-Oriented Programming

Operators in Java

⏱ 3 hr3 topicsInteractive
🎯 By the end: You can use Java's operators, predict the result of prefix/postfix increments, apply precedence rules, and recognise the new and dot operators.

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:

FormOperandsExample
Unary1-a, ++a, !flag
Binary2a + b, a > b
Ternary3(a > b) ? a : b

Types of operators

TypeOperators
Arithmetic+ - * / %
Relational< > <= >= == !=
Logical&& (and) , || (or) , ! (not)
Assignment=
Shorthand+= -= *= /= %=
The ternary operator condition ? valueIfTrue : valueIfFalse is a compact if-else: int big = (a > b) ? a : b; stores the larger of a and b.
Key points
  • 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)
Prefix changes the value before it's used; postfix changes it after. Both leave the variable increased by 1 at the end.
Key points
  • ++ 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)
Key points
  • 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:

  1. Reads two numbers and prints their sum, difference, product and remainder.
  2. Uses the ternary operator to print the larger of the two.
  3. Shows the difference between ++a and a++ by printing the results.
  4. 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.