Boolean Expressions and Comparison Results
What you'll learn: How comparison operators produce boolean results that Java uses to make decisions.
What Are Boolean Expressions?
A boolean expression is any piece of code that evaluates to either true or false. You've already seen boolean variables that hold these values directly, but the real power comes from creating these values on the fly using comparison operators.
Comparison Operators Produce Booleans
When you compare two values, Java answers with a boolean:
int age = 25;
boolean isAdult = age >= 18; // true
boolean isTeen = age < 20; // false
The common comparison operators are:
==equal to!=not equal to<less than>greater than<=less than or equal to>=greater than or equal to
Each comparison produces a boolean result that you can store in a variable or use directly.
Seeing It in Action
int score = 85;
boolean passed = score >= 60; // true
boolean perfect = score == 100; // false
boolean failed = score < 60; // false
boolean notPerfect = score != 100; // true
Think of comparison operators as questions Java asks: "Is 85 greater than or equal to 60?" Java answers with true or false.
Why This Matters
These boolean results are the foundation of control flow—making your program behave differently based on conditions. Right now, you're creating and storing these values. Soon, you'll use them to make decisions in your code.
Key Takeaway: Comparison operators (==, !=, <, >, <=, >=) evaluate relationships between values and always produce a boolean result: true or false.