Lesson 6 — Operations

Operators

Operators are the verbs of Java — they perform calculations, make comparisons, and combine conditions. Understanding operator precedence and the subtle traps (especially integer division) prevents a whole category of bugs.

Arithmetic Operators

Java provides the standard arithmetic operators. The one that surprises beginners most is / — when both operands are integers, Java performs integer division and silently discards the decimal.

JAVA — Arithmetic
int a = 17;
int b = 5;

System.out.println(a + b);   // 22  — addition
System.out.println(a - b);   // 12  — subtraction
System.out.println(a * b);   // 85  — multiplication
System.out.println(a / b);   // 3   — integer division (NOT 3.4!)
System.out.println(a % b);   // 2   — modulus: remainder of 17 ÷ 5

// To get decimal division, at least one operand must be double:
System.out.println((double) a / b);    // 3.4
System.out.println(a / (double) b);    // 3.4
System.out.println(17.0 / 5);         // 3.4

The Modulus Operator

The % operator returns the remainder after integer division. It is more useful than it first appears:

JAVA — Modulus use cases
// Check if a number is even or odd
int n = 42;
if (n % 2 == 0) {
    System.out.println(n + " is even");
} else {
    System.out.println(n + " is odd");
}

// Wrap an index around (e.g. cycling through 5 options)
for (int i = 0; i < 12; i++) {
    System.out.print(i % 5 + " ");  // 0 1 2 3 4 0 1 2 3 4 0 1
}

// Extract the last digit of a number
int year = 2025;
System.out.println("Last digit: " + (year % 10));  // 5

Compound Assignment Operators

These combine an operation with assignment. x += 5 is exactly equivalent to x = x + 5 but shorter and clearer.

JAVA — Compound assignment
int score = 50;
score += 10;  // score = 60
score -= 5;   // score = 55
score *= 2;   // score = 110
score /= 11;  // score = 10
score %= 3;   // score = 1 (remainder of 10 ÷ 3)

System.out.println(score); // 1

// Increment and decrement
int count = 0;
count++;  // count = 1  (post-increment)
count++;  // count = 2
count--;  // count = 1  (post-decrement)
++count;  // count = 2  (pre-increment — difference matters in expressions)

// Pre vs post in an expression:
int x = 5;
System.out.println(x++); // prints 5, THEN increments x to 6
System.out.println(++x); // increments x to 7, THEN prints 7

Comparison Operators

Comparison operators always produce a boolean result. They are the building blocks of all conditional logic.

JAVA — Comparison
int mark = 74;
System.out.println(mark == 74);   // true  — equals
System.out.println(mark != 74);   // false — not equals
System.out.println(mark >  60);   // true  — greater than
System.out.println(mark <  80);   // true  — less than
System.out.println(mark >= 74);   // true  — greater than or equal
System.out.println(mark <= 73);   // false — less than or equal

// CRITICAL: Never compare Strings with == in Java
String a = "hello";
String b = "hello";
System.out.println(a == b);       // unreliable (compares references)
System.out.println(a.equals(b));  // true  — always use .equals() for Strings

Logical Operators

Logical operators combine boolean expressions. Java uses short-circuit evaluation: for &&, if the left side is false, the right side is never evaluated. For ||, if the left side is true, the right side is skipped.

JAVA — Logical operators
boolean hasMatric  = true;
boolean hasLaptop  = false;
int     age        = 22;

// AND — both must be true
if (hasMatric && age >= 18) {
    System.out.println("Eligible for the programme");
}

// OR — at least one must be true
if (hasLaptop || age > 20) {
    System.out.println("Can attend in-person or remotely");
}

// NOT — reverses the boolean
if (!hasLaptop) {
    System.out.println("Please request a loaned device");
}

// Combining conditions — use parentheses for clarity
if ((age >= 18 && age <= 35) && (hasMatric || hasLaptop)) {
    System.out.println("Qualifies for youth programme");
}

Operator Precedence

When multiple operators appear in one expression, Java evaluates them in a defined order. When in doubt, use parentheses — they override precedence and make your intent explicit.

JAVA — Precedence
// Without parentheses — might surprise you
System.out.println(2 + 3 * 4);     // 14, not 20 (* before +)
System.out.println(10 - 2 - 3);    // 5  (left to right)
System.out.println(2 > 1 && 5 < 3); // false (&& after comparisons)

// With parentheses — always clear
System.out.println((2 + 3) * 4);   // 20
System.out.println(2 + (3 * 4));   // 14 — same as without, but explicit

Practice Task

Your Turn

Given three test marks stored as integers (say 65, 72, and 80), calculate and print: (1) the total, (2) the average as a double (watch for integer division!), (3) whether the average is a pass (>= 50) using a boolean, (4) whether all three marks are above 60, (5) the remainder when the total is divided by 7. Add a comment next to each explaining what it demonstrates.

Common Mistakes

  • 7 / 2 equals 3, not 3.5 — cast at least one operand to double before dividing.
  • = is assignment, == is comparison. Writing if (x = 5) instead of if (x == 5) is a logic error (or compile error for non-booleans).
  • Using == to compare Strings — always use .equals().
  • Forgetting that ! binds tightly: !a && b is (!a) && b, not !(a && b).
  • Integer overflow — int silently wraps around when it exceeds ~2.1 billion. Use long for large values.

Professional Tip

Put parentheses around every group of conditions involving both && and || — relying on precedence rules is how subtle logic bugs sneak into production code.

Mini Quiz

What is the result of 17 % 5 in Java?