Notifications
Clear all
Topic starter 02/08/2025 12:01 am
Java boolean statements are the backbone of decision-making logic. Let’s shine a spotlight on how they work 🔦
✅ What is a Boolean?
A boolean is a data type in Java that can only have one of two values:
true // yep, it's true
false // nope, it's false
You use booleans to make choices, test conditions, and control the flow of your code.
🧪 Boolean Variables
You can declare and use boolean variables like this:
boolean isRunning = true;
boolean isNight = false;
🔍 Boolean Expressions (Conditions)
These are statements that evaluate to true
or false
based on comparisons:
int age = 25;
boolean isAdult = age >= 18; // true
Boolean expressions often use:
==
(equal to)!=
(not equal to)>
<
>=
<=
(greater/less than)&&
(AND),||
(OR),!
(NOT)
Example:
boolean canEnter = age > 18 && age < 65;
⚙️ In If-Else Statements
Booleans often control which path your code takes:
boolean isSunny = true;
if (isSunny) {
System.out.println("Grab your sunglasses!");
} else {
System.out.println("Better take an umbrella.");
}
🔁 Boolean Methods
You can write methods that return boolean values:
boolean isEven(int num) {
return num % 2 == 0;
}
Then use it like:
System.out.println(isEven(6)); // prints true