Java Boolean Logic and Boolean Expressions

Class Notes for High School Students

Topics: boolean type • comparisons • logical operators • order of operations • short-circuiting

✅ 1. What is a Boolean?

A boolean is a data type that has only two possible values: true or false.

Booleans are used to make decisions in programs, control flow, and test conditions.

boolean isRaining = true;
boolean finishedHomework = false;

✅ 2. Boolean Expressions

A boolean expression is any expression that evaluates to true or false.

5 > 3        // true
10 == 2      // false
age >= 18    // depends on the value of age
Structure: value operator value
Example: score >= 70

✅ 3. Comparison Operators

These operators compare values and return a boolean result.

Operator Meaning Example
==equal toa == b
!=not equal toa != b
>greater thana > b
<less thana < b
>=greater than or equala >= b
<=less than or equala <= b
int score = 85;
boolean passed = score >= 70;   // true

✅ 4. Logical Operators

Logical operators combine multiple boolean expressions.

AND (&&)

Returns true only if both conditions are true.

(age > 16 && hasLicense)
ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

OR (||)

Returns true if at least one condition is true.

(isWeekend || isHoliday)
ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

NOT (!)

Reverses a boolean value.

!true   // false
!false  // true
boolean loggedIn = false;
if (!loggedIn) {
   System.out.println("Please login.");
}

✅ 5. Using Boolean Logic in IF Statements

Boolean expressions are commonly used in decision making.

Basic IF Statement

if (temperature > 90) {
   System.out.println("It's hot outside!");
}

IF-ELSE Example

if (score >= 70) {
   System.out.println("You passed.");
} else {
   System.out.println("You failed.");
}

✅ 6. Combining Multiple Conditions

if (age >= 16 && hasPermit) {
   System.out.println("Eligible to drive.");
}

✅ 7. Order of Boolean Operations

Java evaluates logical expressions in this order:

  1. Parentheses ()
  2. NOT !
  3. AND &&
  4. OR ||
boolean result = true || false && false;
// evaluates as: true || (false && false) => true || false => true

✅ 8. Short-Circuit Evaluation (Important Concept)

Java may stop evaluating early if the result is already known. This can make code faster and prevent errors.

AND Example

if (false && something()) {
   // something() will NOT run
}

OR Example

if (true || something()) {
   // something() will NOT run
}

✅ 9. Common Beginner Mistakes

❌ Mistake: Using = instead of ==
if (a = 5)   // WRONG
✅ Correct:
if (a == 5)
❌ Mistake: Forgetting parentheses in an if statement
if age > 18   // WRONG
✅ Correct:
if (age > 18)
✅ Tip: Don’t compare booleans to true unnecessarily
if (isReady == true)  // not needed
Better:
if (isReady)

✅ 10. Real-World Example Program

public class BooleanExample {

    public static void main(String[] args) {

        int age = 17;
        boolean hasID = true;

        if (age >= 16 && hasID) {
            System.out.println("Allowed entry.");
        } else {
            System.out.println("Not allowed.");
        }
    }
}

🎯 Summary

Boolean logic allows programs to:

Key operators:

==  !=  >  <  >=  <=
&&  ||  !
Teacher Bonus: Want practice problems, a worksheet, or an AP exam “boolean traps” sheet? Tell me what level (intro / AP CSA) and how many questions you want.