[!NOTE] Operators are the symbols that perform operations on variables and values. You will need to chain these together frequently to create robust boolean conditions.
The 4 Categories of Operators
| Category | Purpose | Examples |
|---|---|---|
| Arithmetic | Math calculations | +, -, *, /, % (Modulo) |
| Assignment | Setting variable values | =, +=, -= |
| Comparison | Evaluating equality | ==, !=, >, <= |
| Logical | Chaining boolean logic | && (AND), ` |
The Modulo Operator (%)
While / gives you the quotient of division, % gives you the Remainder of division. This is extremely useful for determining if a number is Even or Odd!
int remainder = 10 % 3; // Returns 1 (because 10 divided by 3 is 9, remainder 1).
if (number % 2 == 0) {
System.out.println("This number is definitely EVEN.");
}
Logical Short-Circuit Evaluation
Logical operators (&& and ||) possess an intelligent "short-circuit" mechanism to speed up code execution and prevent Null Pointer Exceptions.
If the first condition in an && (AND) statement evaluates to false, Java realizes that the entire condition is doomed to fail. Therefore, it will not even bother evaluating the second condition.
Similarly, if the first condition in an || (OR) statement evaluates to true, Java will not evaluate the second condition, because the overall result is guaranteed to be true.
// Safe Execution Example!
// If 'user' is null, the first check fails.
// Because of the && short-circuit, Java STOPS evaluating.
// It never attempts to run user.isActive(), preventing an app crash!
if (user != null && user.isActive() == true) {
System.out.println("User is logged in safely.");
}
[!CAUTION] If you actually want both sides to evaluate no matter what, use the bitwise
&and|operators instead. But this is extremely rare in web applications.
Operators as Business Rules
Operators are small symbols, but they usually express important rules: whether a user can log in, whether a cart qualifies for free delivery, whether a number is in range, or whether a loop should continue.
Readable Boolean Logic
double cartTotal = 799.0;
boolean isMember = true;
boolean qualifiesByAmount = cartTotal >= 999.0;
boolean qualifiesByMembership = isMember && cartTotal >= 499.0;
if (qualifiesByAmount || qualifiesByMembership) {
System.out.println("Free delivery applied");
}
Splitting the rule into named booleans makes the condition easier to understand and change later.
Integer Division Trap
int correct = 7;
int total = 10;
double wrongPercent = correct / total * 100; // 0.0 because 7 / 10 is 0
double rightPercent = (double) correct / total * 100; // 70.0
When both operands are integers, Java performs integer division. Cast one value to double before dividing if you need a decimal result.
Common Mistakes
- Confusing assignment
=with comparison==. - Forgetting operator precedence and relying on readers to guess.
- Using
&or|when short-circuiting&&or||is intended. - Doing percentage math with integer division by accident.
Mini Practice
Create a login rule: user is allowed only when email is verified, password is correct, and account is not locked. Write it once as one large condition, then rewrite it using named boolean variables.
Practice Lab: Login Rule Builder
Use operators to express a real access rule.
- Create booleans for
isEmailVerified,isPasswordCorrect, andisAccountLocked. - Create a boolean called
canLogin. - Allow login only when email is verified, password is correct, and account is not locked.
- Print different messages for allowed and denied login.
Goal: Practice &&, ||, !, and readable boolean variables.
Revision Checkpoint
- Assignment:
=stores a value. - Comparison:
==,!=,>, and<=produce booleans. - Logical AND:
&&requires both sides to be true. - Logical OR:
||requires at least one side to be true. - Modulo:
%gives the remainder and is useful for even/odd checks.
Before the quiz: Explain why user != null && user.isActive() is safer than checking user.isActive() first.