Logical AND Operator: &&
What You'll Learn
You'll learn how the && operator combines two boolean expressions and only returns true when both sides are true.
The Core Idea
Think of && like a security door that requires two keys to open. If you have only one key—or neither—the door stays locked. Both conditions must be satisfied simultaneously.
The && operator takes two boolean expressions (one on each side) and evaluates them:
- Both true? → Result is
true - Either or both false? → Result is
false
Here's the complete truth table:
true && true // true
true && false // false
false && true // false
false && false // false
Real Examples
int age = 25;
boolean hasLicense = true;
// Can this person rent a car? (must be 21+ AND have a license)
boolean canRent = (age >= 21) && hasLicense; // true
int temperature = 75;
// Is it a nice day? (temperature between 70 and 80)
boolean niceDay = (temperature >= 70) && (temperature <= 80); // true
int score = 85;
// Did the student fail? (score less than 60 AND greater than 0)
boolean failed = (score < 60) && (score > 0); // false (first condition is false)
Notice that && connects two complete boolean expressions. Each side can use the comparison operators you learned earlier (<, >, ==, etc.).
Key Takeaway: The && operator requires both operands to be true to produce true; if either side is false, the entire expression becomes false. Think "must satisfy ALL conditions."