Control Flow & Loops

Booleans & Comparison Operators

15 min Lesson 1 of 14

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.

boolean isLoggedIn = true; boolean hasPermission = false; System.out.println(isLoggedIn); // true System.out.println(hasPermission); // false

The variable name matters. Use names that read as yes/no questions: isReady, hasError, canEdit. This makes your code almost self-documenting.

Naming convention: prefix boolean variables and methods with 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.

int age = 20; System.out.println(age == 20); // true System.out.println(age != 18); // true System.out.println(age < 18); // false System.out.println(age > 18); // true System.out.println(age <= 20); // true System.out.println(age >= 21); // 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:

int score = 85; boolean passed = score >= 60; boolean perfect = score == 100; System.out.println(passed); // true System.out.println(perfect); // false

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 grade = 'B'; System.out.println(grade == 'B'); // true — same character value System.out.println(grade < 'C'); // true — 'B' has a smaller Unicode value than 'C' System.out.println(grade > 'A'); // true double price = 9.99; System.out.println(price < 10.0); // true
Characters are numbers under the hood. Each 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.

String a = new String("hello"); String b = new String("hello"); System.out.println(a == b); // false — two different objects in memory System.out.println(a.equals(b)); // true — same characters
Always use .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.

int x = 7; int y = 10; boolean xIsSmaller = x < y; // true boolean theyAreEqual = x == y; // false boolean xIsNotTen = x != 10; // true boolean sumIsPositive = (x + y) > 0; // true

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.