Operators & Expressions
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.).
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.
= 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.
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.
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.
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).
++ 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:
++/--(postfix), then++/--(prefix),!*/%+-<><=>===!=&&||=+=-=etc. (lowest)
(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:
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.