Logical Operators in Java
Java has three logical operators for combining or inverting boolean expressions: && (AND), || (OR), and ! (NOT). && evaluates to true only when both sides are true; || evaluates to true when at least one side is; ! flips a boolean's value entirely.
&& and || are short-circuiting: if the left side of && is already false, or the left side of || is already true, Java never even evaluates the right side, since the overall answer is already decided. This isn't just a performance detail — it's routinely used on purpose, such as checking an object isn't null before calling a method on it within that same condition.
AND (&&)
True only when both operands are true.
OR (||)
True when at least one operand is true.
NOT (!)
Inverts a single boolean: !true is false, and !false is true.
Short-circuit evaluation
&& skips evaluating its right side once the left side is false, and || skips its right side once the left side is true, since the result is already known.
A deliberate use of short-circuiting
Writing a null check and a method call in the same && condition relies on the null check running first, so the method call never executes if the object actually is null.
public class LogicalDemo { public static void main(String[] args) { String name = null; boolean safe = name != null && name.length() > 0; System.out.println("Safe check: " + safe); System.out.println("NOT true: " + (!true)); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.