boolean is a primitive data type, meaning it holds a single, simple value at a time. But unlike numbers or characters, a boolean can only ever be one of two things: true or false. No in-between, no other options — just an on/off switch built right into the language.
Declaring Boolean Variables
Here's how you create and store boolean values:
boolean isNight = true;
boolean isMonday = false;
In this example, isNight holds true and isMonday holds false. Simple as that. But the real power of booleans shows up when you start using them to make decisions in your code — which is exactly what they're built for.
Using Booleans to Evaluate Conditions
Most of the time, you won't set a boolean value directly like above. Instead, you'll get one back as the result of a comparison — things like checking if two values are equal, or if one number is bigger than another. Take a look at this example:
class TrueFalse {
public static void main(String[] args) {
int value1 = 1;
int value2 = 2;
if (value1 == value2)
System.out.println("value1 == value2");
if (value1 != value2)
System.out.println("value1 != value2");
if (value1 > value2)
System.out.println("value1 > value2");
if (value1 < value2)
System.out.println("value1 < value2");
if (value1 <= value2)
System.out.println("value1 <= value2");
}
}
Let's walk through what's actually happening here. Each if statement is quietly evaluating a comparison behind the scenes, and that comparison produces a boolean value — either true or false — even though you never see the word "boolean" written anywhere in the if lines.
Since value1 is 1 and value2 is 2:
value1 == value2→ evaluates tofalse, since 1 doesn't equal 2, so this line gets skipped.value1 != value2→ evaluates totrue, since 1 really isn't equal to 2, so this one prints.value1 > value2→ evaluates tofalse, since 1 isn't greater than 2, so it's skipped.value1 < value2→ evaluates totrue, since 1 is indeed less than 2, so this prints.value1 <= value2→ evaluates totrueas well, since 1 is less than or equal to 2, so this prints too.
So when you run this program, the output looks like this:
value1 != value2
value1 < value2
value1 <= value2
Why This Matters
Booleans might seem almost too simple to matter, but they're the backbone of just about every decision your program makes. Every if statement, every loop condition, every logical check ultimately boils down to a true or false value. Once you get comfortable with how comparisons produce booleans behind the scenes, reading and writing conditional logic in Java becomes a lot more intuitive.