Short-circuit boolean operators
What you will learn
Boolean operators combine decisions. Use && when both conditions must hold, || when at least one must hold and ! to reverse a boolean. The conditional operators can avoid evaluating an unnecessary or unsafe second expression.
public class Main { public static void main(String[] args) { int seats = 3; boolean paid = true; boolean cancelled = false; boolean allowed = seats > 0 && paid && !cancelled; System.out.println(allowed); System.out.println(cancelled || seats == 0); }}Expected output:
truefalseAll required conditions hold, and neither cancellation nor full capacity applies.
How it works
a && b evaluates b only if a is true; a || b evaluates b only if a is false. Both operands must be boolean or convertible by unboxing to boolean. !a flips the truth value. Java also supports boolean &, | and ^; unlike && and ||, these evaluate both operands when the first completes normally. An expression such as name.length() calls a method on a String and returns its UTF-16 length. Guard it with name != null && name.length() > 0. Similarly check a divisor before dividing. The conditional operator condition ? valueIfTrue : valueIfFalse evaluates exactly one result expression after the condition. Its result has a compile-time type; start with branches of the same type. This is an expression, not a replacement for every multi-step if statement. Parenthesize mixed AND/OR conditions to make the policy visible.
public class Main { public static void main(String[] args) { String label = null; int groups = 0; System.out.println(label != null && label.length() > 0); System.out.println(groups == 0 || 24 / groups > 2); }}Expected output:
falsetrueThe first right operand would dereference null and the second would divide by zero. Neither executes.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { int balance = 8; int required = 10; String message = balance >= required ? "Accepted" : "Insufficient"; System.out.println(message); System.out.println(balance > 0 ? 20 / balance : 0); }}Expected output:
Insufficient2The first condition selects a string; the second selects an integer expression. Only the chosen branch is evaluated.
Common mistakes
label.length() > 0 && label != null checks too late. Reversing the operands is not equivalent when one can fail. Do not replace && with & in a null guard: eager boolean AND evaluates the right side.
Try it yourself
For a parcel, print true only if its tracking label is non-null and nonempty and its weight is greater than 0 and at most 20. Test label "B7", weight 20, then null label and weight 5. Print "Ready" or "Review" using a conditional expression.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Short-circuit boolean operators — knowledge check
Apply short-circuit boolean operators to examples and boundary conditions.
When does `false && expression` evaluate expression?
When is the right operand of `||` skipped?
Which check prevents a null String named label from being dereferenced?
What is `false ? 9 : 4`?
Which boolean operator evaluates both operands if the left completes normally?