Learn Java
import com.learnjava.operators.OpLogical;

Logical Operators in Java

@Concept

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.

@HowItWorks
1

AND (&&)

True only when both operands are true.

2

OR (||)

True when at least one operand is true.

3

NOT (!)

Inverts a single boolean: !true is false, and !false is true.

4

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.

5

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.

@Example
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));
}
}
Output
Safe check: false
NOT true: false
1name != null evaluates first and is false, since name really is null.
2Because the left side of && is already false, Java short-circuits and never calls name.length() at all — which would otherwise throw a NullPointerException.
3safe ends up false, which the first println reports.
4!true simply flips true to false, which the second println shows directly.
@UseCases
Guarding a method call behind a null check in the same condition, safely
Combining multiple conditions into one, like checking an age range with two comparisons joined by &&
Understanding why the right side of an && or || sometimes never runs at all
Inverting a boolean flag's meaning with !

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.