00:00

Logical Operators in Java – AND, OR and NOT with Examples

Logical operators combine or invert boolean conditions. They let a program make decisions such as "the user may enter if they are an adult and have an ID" or "show a discount if the customer is a member or has a coupon".

Java has three logical operators: && (AND), || (OR) and ! (NOT). This tutorial explains each one, shows truth tables, and covers short-circuit evaluation, the difference between && and &, De Morgan's laws and precedence.

1. The Three Logical Operators

OperatorNameResult is true when…Example
&&Logical ANDboth conditions are trueage >= 18 && hasId
||Logical ORat least one condition is trueisVip || age < 25
!Logical NOTthe condition is false!isVip
LogicalBasics.java
public class LogicalBasics {
    public static void main(String[] args) {
        int age = 22;
        boolean hasId = true;
        boolean isVip = false;

        boolean canEnter = age >= 18 && hasId;          // AND: both must be true
        boolean freeDrink = isVip || age < 25;          // OR: at least one must be true
        boolean mustPay = !isVip;                       // NOT: flips the value

        System.out.println("canEnter  = " + canEnter);
        System.out.println("freeDrink = " + freeDrink);
        System.out.println("mustPay   = " + mustPay);

        // Combining operators (use parentheses for clarity)
        boolean discount = (age < 25 || isVip) && hasId;
        System.out.println("discount  = " + discount);
    }
}
Output
canEnter  = true
freeDrink = true
mustPay   = true
discount  = true

2. Truth Table

ABA && BA || B!A
falsefalsefalsefalsetrue
falsetruefalsetruetrue
truefalsefalsetruefalse
truetruetruetruefalse

Logical operators work only on boolean values. Unlike C, Java does not treat numbers as true or false, so if (count && total) does not compile when they are integers.

3. Short-Circuit Evaluation

&& and || evaluate the right operand only if it is needed:

  • For A && B, if A is false, the result must be false, so B is skipped.
  • For A || B, if A is true, the result must be true, so B is skipped.
ShortCircuit.java
public class ShortCircuit {
    static boolean check(String name, boolean result) {
        System.out.println("  evaluated " + name);
        return result;
    }

    public static void main(String[] args) {
        System.out.println("false && ...");
        boolean a = check("left", false) && check("right", true);   // right side skipped
        System.out.println("result: " + a);

        System.out.println("true || ...");
        boolean b = check("left", true) || check("right", false);   // right side skipped
        System.out.println("result: " + b);

        System.out.println("false & ... (no short-circuit)");
        boolean c = check("left", false) & check("right", true);    // both sides run
        System.out.println("result: " + c);

        // Practical use: a null check protects the method call
        String input = null;
        if (input != null && !input.isBlank()) {
            System.out.println("Got input");
        } else {
            System.out.println("No input (and no NullPointerException)");
        }
    }
}
Output
false && ...
  evaluated left
result: false
true || ...
  evaluated left
result: true
false & ... (no short-circuit)
  evaluated left
  evaluated right
result: false
No input (and no NullPointerException)

Short-circuiting is more than a speed-up: it lets the left condition guard the right one, as in the null check above. If the order were reversed (!input.isBlank() && input != null), the program would throw a NullPointerException.

4. && vs & and || vs |

Feature&& / ||& / |
Operand typesboolean onlyboolean or integers (bitwise)
Short-circuitYesNo: both sides are always evaluated
Typical useConditions in if and loopsBit manipulation, or when both sides must run

In conditions, almost always use && and ||. See Bitwise Operators for &, | and ^.

5. De Morgan's Laws, XOR and Precedence

ConditionRules.java
public class ConditionRules {
    public static void main(String[] args) {
        boolean raining = true, haveUmbrella = false;

        // De Morgan's laws: !(A && B) == !A || !B   and   !(A || B) == !A && !B
        System.out.println("!(raining && haveUmbrella) = " + !(raining && haveUmbrella));
        System.out.println("!raining || !haveUmbrella  = " + (!raining || !haveUmbrella));

        // XOR on booleans: true when exactly one is true
        boolean cardPayment = true, cashPayment = false;
        System.out.println("exactly one payment method: " + (cardPayment ^ cashPayment));

        // && binds tighter than ||
        boolean x = true || false && false;      // true || (false && false)
        boolean y = (true || false) && false;
        System.out.println("true || false && false   = " + x);
        System.out.println("(true || false) && false = " + y);

        // Range check
        int score = 75;
        System.out.println("score in 60..100: " + (score >= 60 && score <= 100));
    }
}
Output
!(raining && haveUmbrella) = true
!raining || !haveUmbrella  = true
exactly one payment method: true
true || false && false   = true
(true || false) && false = false
score in 60..100: true
  • De Morgan's laws: !(A && B) equals !A || !B, and !(A || B) equals !A && !B. They help simplify negated conditions.
  • Exclusive OR: ^ on booleans is true when exactly one side is true.
  • Precedence: ! binds tightest, then &&, then ||. Relational operators such as >= are evaluated before all of them, so score >= 60 && score <= 100 needs no extra parentheses.

6. Writing Clear Conditions

  • Give complex conditions a name: boolean isEligible = age >= 18 && hasId;.
  • Put cheap or guarding checks first, so short-circuiting saves work and prevents errors.
  • Use parentheses when mixing && and ||.
  • Avoid double negatives: if (isActive) is clearer than if (!isInactive).
  • Do not compare booleans with true: write if (done), not if (done == true).

7. Summary

  • && is true only if both sides are true; || is true if either side is true; ! inverts.
  • && and || short-circuit, so the right side may not run.
  • &, | and ^ also work on booleans but always evaluate both sides.
  • Precedence: ! > && > ||; use parentheses for clarity.

Related Tutorials