Skip to lesson

Logical operators: &&, || and !

A logical operator joins true/false values. && (AND) needs both sides to be true, || (OR) needs at least one, and ! (NOT) flips a value.

&& (AND): both must be true

To sit an exam, a student needs 75% attendance and paid fees. One comparison cannot check both.

The AND operator && gives true only when both sides are true.

Truth table for a && b: only true and true gives true; every other pair gives false.

Example 1 · Can the student sit the exam?

Exam.javaJava
public class Exam {    public static void main(String[] args) {        int attendance = 80;        boolean hasPaidFees = false;        boolean canSit = attendance >= 75 && hasPaidFees;        System.out.println("Can sit exam: " + canSit);        boolean isValid = attendance >= 0 && attendance <= 100;        System.out.println("Valid attendance: " + isValid);    }}
Output
Can sit exam: false
Valid attendance: true

What this code does

  • Attendance is fine, but the fees are not paid, so && gives false.
  • The range check is two comparisons joined with &&: 80 is at least 0 and at most 100.

The maths style 18 <= age <= 60 does not compile. Write two comparisons and join them with &&: age >= 18 && age <= 60.

|| (OR): at least one must be true

A cinema gives a discount to students or senior citizens, and someone who is both gets it too.

The OR operator || gives true when at least one side is true, and false only when both are false.

Truth table for a || b: true whenever at least one side is true; only false and false gives false.

Example 2 · Who gets the discount?

Discount.javaJava
public class Discount {    public static void main(String[] args) {        boolean riyaIsStudent = true;        int riyaAge = 20;        System.out.println("Riya: " + (riyaIsStudent || riyaAge >= 60));        boolean raviIsStudent = false;        int raviAge = 45;        System.out.println("Ravi: " + (raviIsStudent || raviAge >= 60));    }}
Output
Riya: true
Ravi: false

What this code does

  • Riya is a student, so || gives true; one true side is enough.
  • Ravi is not a student and is under 60. Both sides are false, so || gives false.
The | key is usually above Enter; press it with Shift.

! (NOT): flip the value

Your app stores hasPaidFees, but a reminder screen needs the opposite: are the fees still pending?

The NOT operator ! flips a value: !true is false, so !hasPaidFees is true exactly when the fees are pending.

Truth table for !a: !true is false and !false is true.

&&, || and ! work only on boolean values. !marks with an int gives the error bad operand type int for unary operator '!'. Flip the switches below to see all three at work.

Experiment

Truth switches

Turn a and b on and off. Find the only case where a || b is false.

boolean a = true;
boolean b = false;
a && b
false
a || b
true
!a
false
!b
true

Short-circuit: Java stops early

An app checks totalMarks / students > 50, but when students is 0 the division stops the program, as in Arithmetic operators (opens in a new tab).

Short-circuit: Java skips the right side when the left side already decides the answer.

Picture 1 / 3
In students != 0 && totalMarks / students > 50, Java looks at the left side first. students is 0.
Picture 1 / 3

Example 3 · Short-circuit keeps you safe

SafeDivide.javaJava
public class SafeDivide {    public static void main(String[] args) {        int students = 0;        int totalMarks = 0;        boolean goodAverage = students != 0 && totalMarks / students > 50;        System.out.println("Good average: " + goodAverage);    }}
Output
Good average: false

What this code does

  • students != 0 is false, so && already knows the answer is false and stops there.
  • The division never runs, so there is no / by zero error.

With ||, a true left side decides the answer, so the right side is skipped too. Put the safety check on the left: swap the two sides and the division runs first, and the program stops.

Java also has & and |, which always run both sides. For conditions, use && and ||.

Next step

Check your understanding

10 quick questions