Booleans & Comparison Operators
Booleans & Comparison Operators
Every decision a program makes comes down to a single question: is this true or false? Java answers that question with the boolean type and a set of comparison operators. Before you can write a single if statement or loop, you need to understand this foundation thoroughly.
The boolean Type
In Java, boolean is a primitive type that holds exactly one of two values: true or false. There is no zero, no null, no empty string — just those two.
The variable name matters. Use names that read as yes/no questions: isReady, hasError, canEdit. This makes your code almost self-documenting.
is, has, can, or should. Java itself follows this in its standard library — String.isEmpty(), List.contains(), etc.
Comparison Operators
Comparison operators evaluate two values and produce a boolean result. Java has six of them:
==— equal to!=— not equal to<— less than>— greater than<=— less than or equal to>=— greater than or equal to
Each of these expressions is called a boolean expression — it evaluates to either true or false.
Storing Comparison Results
Because a comparison produces a boolean, you can store the result in a variable just like any other value. This is a common and clean pattern:
Storing comparisons in named variables makes complex conditions easier to read later — instead of a wall of operators, the variable name explains the intent.
Comparing Primitive Types
The six comparison operators work directly on Java's numeric primitives: int, long, double, float, short, byte, and char. The comparison is always by value.
char is stored as a Unicode code point — an integer. 'A' is 65, 'B' is 66, and so on. That is why you can compare characters with < and >.
The == Trap with Objects
The == operator compares values for primitives. But for objects (like String), == checks whether two variables point to the same object in memory, not whether their contents are equal. This is one of the most common beginner mistakes in Java.
.equals() to compare String contents. Using == on strings sometimes appears to work (when the JVM reuses string literals from its pool), but it is unreliable and will cause hard-to-find bugs. This lesson covers primitives, but keep this rule in mind for the moment you touch strings.
Boolean Expressions as Building Blocks
A boolean expression can be used anywhere Java expects a boolean: stored in a variable, passed to a method, or placed directly inside an if condition. In the next lesson you will wire these expressions into if/else branches. For now, practice reading and evaluating them mentally.
Summary
The boolean type holds exactly true or false. The six comparison operators — ==, !=, <, >, <=, >= — compare two primitive values and produce a boolean result. You can store that result in a variable or use it directly. Remember that == compares value for primitives, but for objects you need .equals(). Everything in control flow — every branch, every loop — is driven by these simple true/false expressions.