Operators

Operators are the small symbols that do the actual work in an expression — adding numbers, comparing values, combining conditions. Java's set is close to what you'd find in C, and once you've got a handle on a few gotchas around integers, most of it behaves exactly the way you'd expect.

Arithmetic operators

+, -, *, /, and % handle the basics. The one that trips people up is / when both operands are integers — it doesn't round, it truncates toward zero, discarding the remainder entirely.

</> ReceiptTotal.java
public class ReceiptTotal {
    public static void main(String[] args) {
        int totalCents = 1275;
        int dollars = totalCents / 100;
        int remainingCents = totalCents % 100;

        System.out.println("Dollars: " + dollars);
        System.out.println("Cents: " + remainingCents);
    }
}
Output
Dollars: 12
Cents: 75

That pairing of / and % on integers is a genuinely useful trick — the modulo operator (%) gives you what's left over after division, which is exactly what you need for splitting a total into whole units and a remainder, or for checking whether a number is even (n % 2 == 0).

Comparison and logical operators

Comparisons — ==, !=, <, >, <=, >= — always produce a boolean. Logical operators combine booleans: && for "and," || for "or," ! to flip one.

</> LoanEligibility.java
public class LoanEligibility {
    public static void main(String[] args) {
        int creditScore = 710;
        double annualIncome = 52000;
        boolean hasExistingDebt = false;

        boolean eligible = creditScore >= 680 && annualIncome > 40000 && !hasExistingDebt;
        System.out.println("Eligible: " + eligible);
    }
}
Output
Eligible: true

&& and || short-circuit: in an && chain, Java stops checking as soon as one condition is false, since the overall result can't be true anymore. That's not just an efficiency detail — it means you can safely write account != null && account.getBalance() > 0, trusting that the second half never runs if account is null.

== on objects is a trap worth naming early

== compares primitives by value, which is what you'd expect. But on objects — including String — it compares whether two variables point to the exact same object in memory, not whether their contents look equal. Comparing strings for equal content needs .equals() instead, which you'll see in the next lesson.

Assignment shortcuts

Java offers compound assignment operators that combine an operation with an assignment: +=, -=, *=, /=, %=. It also has ++ and -- for adding or subtracting exactly one.

</> Inventory.java
public class Inventory {
    public static void main(String[] args) {
        int stock = 40;
        stock -= 15;   // fifteen units sold
        stock += 8;    // eight units restocked
        stock++;       // one unit returned

        System.out.println("Stock on hand: " + stock);
    }
}
Output
Stock on hand: 34

stock -= 15; is exactly equivalent to stock = stock - 15; — the compound form just saves you from typing the variable name twice. It reads naturally once you're used to it: "decrease stock by 15."

Prefix vs. postfix

++stock and stock++ both add one, but they differ in what the expression itself evaluates to when used inline. stock++ (postfix) returns the old value and then increments; ++stock (prefix) increments first and returns the new value:

</> PrePostDemo.java
int a = 5;
int b = a++;   // b gets 5, then a becomes 6
int c = 5;
int d = ++c;   // c becomes 6, then d gets 6

System.out.println("a=" + a + " b=" + b);
System.out.println("c=" + c + " d=" + d);
Output
a=6 b=5
c=6 d=6
Note: when in doubt about how an expression will be evaluated, add parentheses. a + b * c follows the usual math precedence (multiplication before addition), but relying on memorized precedence tables to read someone else's code six months from now is a waste of your own time — a + (b * c) costs nothing and removes the ambiguity for the next reader.