Java Fundamentals

Operators & Expressions

15 min Lesson 4 of 14

Operators & Expressions

An expression is any combination of variables, values, and operators that Java evaluates to produce a single value. Understanding operators is essential because almost every line of useful Java code contains one. In this lesson you will learn each category of operator, see it working in a small runnable example, and discover how Java decides which operation to perform first when several operators appear together.

Arithmetic Operators

Arithmetic operators perform math on numeric types (int, double, etc.).

public class ArithmeticDemo { public static void main(String[] args) { 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 (truncates) System.out.println(a % b); // 2 — modulus (remainder) } }
Integer division truncates, it does NOT round. 17 / 5 is 3, not 3.4. If you need a decimal result, at least one operand must be a double: 17.0 / 5 gives 3.4.

The modulus operator % returns the remainder after division. It is extremely useful — checking whether a number is even (n % 2 == 0), cycling through array indices, and many other patterns all rely on it.

Relational (Comparison) Operators

Relational operators compare two values and always produce a boolean result (true or false). They are the backbone of every if statement and loop condition.

int x = 10; int y = 20; System.out.println(x == y); // false — equal to System.out.println(x != y); // true — not equal to System.out.println(x < y); // true — less than System.out.println(x > y); // false — greater than System.out.println(x <= y); // true — less than or equal System.out.println(x >= y); // false — greater than or equal
== vs = : A single equals sign = assigns a value. A double equals sign == tests equality. Writing if (x = 5) instead of if (x == 5) is a classic bug — fortunately Java catches it as a compile error for non-boolean types.

Logical Operators

Logical operators combine or invert boolean values. You use them to build compound conditions.

boolean isAdult = true; boolean hasTicket = false; // AND — both must be true System.out.println(isAdult && hasTicket); // false // OR — at least one must be true System.out.println(isAdult || hasTicket); // true // NOT — inverts the value System.out.println(!isAdult); // false

Java uses short-circuit evaluation for && and ||. With &&, if the left side is false the right side is never evaluated — the result is already false. With ||, if the left side is true the right side is skipped. This matters when the right side has a side effect (such as a method call) or could throw an exception.

Guard with && to avoid NullPointerException: if (user != null && user.isActive()) — because of short-circuit evaluation, user.isActive() is only called when user is not null.

Assignment Operators

The basic assignment operator = stores a value in a variable. Java also provides compound assignment operators that combine an arithmetic operation with assignment, making code shorter and easier to read.

int score = 100; score += 10; // score = score + 10 → 110 score -= 5; // score = score - 5 → 105 score *= 2; // score = score * 2 → 210 score /= 3; // score = score / 3 → 70 (integer division) score %= 8; // score = score % 8 → 6 System.out.println(score); // 6

Increment & Decrement Operators

Because adding or subtracting 1 is so common, Java has dedicated operators: ++ (increment) and -- (decrement). Each comes in two forms: prefix (operator before the variable) and postfix (operator after the variable).

int n = 5; // Postfix: use the current value FIRST, then change it System.out.println(n++); // prints 5, then n becomes 6 System.out.println(n); // 6 // Prefix: change the value FIRST, then use it System.out.println(++n); // n becomes 7, then prints 7 System.out.println(n); // 7
When you use ++ or -- as a standalone statement (not inside a larger expression), prefix and postfix behave identically. The difference only matters when the expression's resulting value is used immediately — for example, as an argument to a method or the right-hand side of an assignment.

Operator Precedence

When multiple operators appear in one expression, Java follows a precedence table — similar to the order-of-operations rule you learned in mathematics (multiplication before addition). The most important rules to memorise:

  1. ++ / -- (postfix), then ++ / -- (prefix), !
  2. *  /  %
  3. +  -
  4. <  >  <=  >=
  5. ==  !=
  6. &&
  7. ||
  8. =  +=  -= etc. (lowest)
int result = 3 + 4 * 2; // Multiplication first: 4 * 2 = 8, then 3 + 8 = 11 System.out.println(result); // 11 result = (3 + 4) * 2; // Parentheses override: 3 + 4 = 7, then 7 * 2 = 14 System.out.println(result); // 14
When in doubt, use parentheses. They cost nothing, make your intent explicit, and prevent subtle precedence bugs. Writing (a > b) && (c != 0) is clearer than a > b && c != 0, even though both compile to the same result.

Putting It All Together

Here is a small program that combines several operators to determine whether a student passes an exam:

public class PassFail { public static void main(String[] args) { int score = 72; int minPass = 50; boolean attended = true; // compound condition using relational + logical operators boolean passed = attended && (score >= minPass); // compound assignment to add a bonus if (passed) { score += 5; // bonus for attending } System.out.println("Final score: " + score); // 77 System.out.println("Passed: " + passed); // true } }

Summary

Java's operators fall into five practical groups: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), and increment/decrement (++ --). Operator precedence determines evaluation order — use parentheses liberally to make expressions unambiguous. In the next lesson you will see how Java automatically widens or narrows values when mixing types in the same expression.