🌱BeginnerOperators
Logical Operators
Combine boolean conditions with && (AND), || (OR), and ! (NOT) to express complex logic.
Overview
Logical operators let you combine multiple boolean conditions into one expression. They are used constantly in if statements, loops, and validations. Java has three: && (AND), || (OR), and ! (NOT).
&& returns true only if BOTH conditions are true. || returns true if AT LEAST ONE condition is true. ! inverts a boolean: !true = false, !false = true. Java uses "short-circuit" evaluation - if the first condition of && is false, the second is never evaluated; if the first condition of || is true, the second is never evaluated.
Key Points
- •&& (AND): both conditions must be true for the whole expression to be true
- •|| (OR): at least one condition must be true
- •! (NOT): flips true to false and vice versa
- •Short-circuit: && stops evaluating at the first false; || stops at the first true
- •Precedence: ! > && > || ; use parentheses for clarity with mixed operators
- •Combine with comparison: `age >= 18 && age <= 65` means "between 18 and 65"
Examples
AND, OR, and NOT truth tables
public class Main {
public static void main(String[] args) {
boolean t = true, f = false;
System.out.println(t && t); // true
System.out.println(t && f); // false
System.out.println(f && t); // false
System.out.println(f && f); // false
System.out.println(t || f); // true
System.out.println(f || f); // false
System.out.println(!t); // false
System.out.println(!f); // true
}
}&& is true only when BOTH sides are true. || is false only when BOTH sides are false.
Logical operators in conditions
public class Main {
public static void main(String[] args) {
int age = 25;
boolean hasID = true;
// AND: must be adult AND have ID
if (age >= 18 && hasID) {
System.out.println("Access granted");
}
// OR: can enter if VIP OR age >= 21
boolean isVip = false;
if (isVip || age >= 21) {
System.out.println("Lounge access granted");
}
// NOT: flip a condition
boolean isDoorLocked = false;
if (!isDoorLocked) {
System.out.println("Door is open");
}
}
}&& and || evaluate left to right and short-circuit.
Short-circuit evaluation prevents errors
public class Main {
public static void main(String[] args) {
String name = null;
// Without short-circuit, name.isEmpty() would throw NullPointerException
// With &&, if name == null is true (name IS null), isEmpty() is never called
if (name != null && name.isEmpty()) {
System.out.println("Name is empty");
} else {
System.out.println("Name is null or has content");
}
}
}Put the "cheap" or "safe" check first in && to benefit from short-circuit evaluation.
Common Mistakes
- Using & instead of && or | instead of || - single & and | do not short-circuit and always evaluate both sides
- Operator precedence mistake: `a || b && c` evaluates as `a || (b && c)` - use parentheses: `(a || b) && c`
- Writing `if (flag == true)` - just write `if (flag)`. Similarly, `if (flag == false)` → `if (!flag)`
Note
For readability, store complex conditions in descriptively named booleans: `boolean canEnter = age >= 18 && hasTicket;` then `if (canEnter) {...}`.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editor…
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4