Java Boolean Expressions — true, false & Conditions
Every decision in a program boils down to a boolean expression — something that evaluates to true or false. Before we learn if-else, while, or for, we need to master booleans.
The boolean Type
boolean isActive = true;
boolean isDeleted = false;
System.out.println(isActive); // true
System.out.println(isDeleted); // false
A boolean can only hold two values: true or false. No 0 or 1, no "yes" or "no" — just true and false.
Comparison Operators Produce Booleans
int a = 10, b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a < b); // true
System.out.println(a > b); // false
System.out.println(a <= 10); // true
System.out.println(a >= 20); // false
Every comparison expression returns a boolean.
Combining Booleans with Logical Operators
AND (&&) — both must be true
int age = 25;
boolean hasLicense = true;
boolean canDrive = (age >= 18) && hasLicense; // true
OR (||) — at least one must be true
boolean isWeekend = true;
boolean isHoliday = false;
boolean dayOff = isWeekend || isHoliday; // true
NOT (!) — inverts the value
boolean isLoggedIn = false;
System.out.println(!isLoggedIn); // true
Truth Tables
AND (&&)
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
OR (||)
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
NOT (!)
| A | !A |
|---|---|
| true | false |
| false | true |
Short-Circuit Evaluation
Java stops evaluating as soon as the result is determined:
// && — if left is false, right is NEVER evaluated
if (false && someExpensiveMethod()) {
// someExpensiveMethod() never runs
}
// || — if left is true, right is NEVER evaluated
if (true || someExpensiveMethod()) {
// someExpensiveMethod() never runs
}
Practical Use: Null Safety
String name = null;
// Safe — if name is null, length() is never called
if (name != null && name.length() > 0) {
System.out.println(name);
}
Without short-circuit evaluation, name.length() would throw a NullPointerException.
Complex Boolean Expressions
You can chain multiple conditions:
int age = 25;
double income = 50000;
boolean hasGoodCredit = true;
boolean loanApproved = (age >= 21) && (income >= 30000) && hasGoodCredit;
Use parentheses to make intent clear:
// Confusing
boolean result = a > 5 && b < 10 || c == 3;
// Clear
boolean result = (a > 5 && b < 10) || (c == 3);
Boolean Methods
Methods can return booleans — by convention, they start with is, has, or can:
public static boolean isEven(int n) {
return n % 2 == 0;
}
public static boolean isInRange(int n, int min, int max) {
return n >= min && n <= max;
}
// Usage
if (isEven(42)) {
System.out.println("Even!");
}
Comparing Strings
Never use == for string comparison:
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false ← compares references
System.out.println(a.equals(b)); // true ← compares content
For case-insensitive comparison:
"Hello".equalsIgnoreCase("hello"); // true
Boolean Pitfalls
1. Using = instead of ==
// WRONG — this is assignment, not comparison
if (x = 5) { } // compile error (Java catches this!)
// CORRECT
if (x == 5) { }
2. Redundant boolean comparisons
// Bad
if (isActive == true) { }
if (isDeleted == false) { }
// Good
if (isActive) { }
if (!isDeleted) { }
3. De Morgan’s Laws
These are useful for simplifying expressions:
!(A && B) is the same as !A || !B
!(A || B) is the same as !A && !B
// These are equivalent:
if (!(age >= 18 && hasID)) { }
if (age < 18 || !hasID) { }
Quick Exercises
- Write a boolean expression that’s
trueif a number is between 1 and 100 (inclusive). - Write a method
isLeapYear(int year)that returnstruefor leap years. - Simplify:
!(x > 5 && y < 10)using De Morgan’s Law.
Summary
booleanholdstrueorfalse- Comparison operators (
==,!=,<,>,<=,>=) return booleans - Logical operators (
&&,||,!) combine booleans - Short-circuit evaluation skips unnecessary work
- Use
.equals()for string comparison, not== - Name boolean methods with
is,has,canprefixes
What’s Next?
Now that you understand boolean expressions, you’re ready for decision-making — the if-else statement: If-Else in Java.