📝 What You’ll Learn

  • What booleans are
  • Declaring and assigning boolean values
  • Boolean expressions
  • Using booleans in if statements
  • Logical operators (&&, ||, !)

🔍 What is a Boolean?

A boolean is a data type that can hold only two values:

true
false

It’s used to represent yes/no, on/off, true/false logic in your code.

✅ Declaring Boolean Variables

boolean isJavaFun = true;
boolean isCold = false;

You can assign boolean values directly or as the result of expressions.

🔢 Boolean Expressions

Boolean values often come from comparing values:

int x = 10;
int y = 20;

boolean result = x < y;  // true

Other examples:

x == y     // false
x != y     // true
x >= 5     // true

🧠 Using Booleans in if Statements

boolean isRaining = true;

if (isRaining) {
    System.out.println("Take an umbrella!");
} else {
    System.out.println("Enjoy the sun!");
}

Or inline:

if (x > y) {
    // do something
}

🧮 Logical Operators

| Operator | Meaning | Example | |-||| | && | AND | x > 5 && y < 10 | | || | OR | x > 5 || y < 10 | | ! | NOT (negation) | !isRaining |

Example:

if (x > 5 && y < 20) {
    System.out.println("x and y are within range");
}

🤯 Common Mistakes

  • Using = instead of == in comparisons

    if (x = 5) { }  // ❌ Assignment, not comparison
  • Forgetting to initialize the boolean

  • Misusing negation (!) and parentheses

📘 Recap

  • Booleans hold true or false
  • Commonly used in conditions and logic
  • Use comparison and logical operators to create boolean expressions