Java Boolean Examples
Real Life Example
Let's use booleans in a real-life example where we want to find out if a person is old enough to vote.
In the example below, we use the >= comparison operator to find out if the age (25) is greater than OR
equal to the voting age limit, which is set to 18:
Example
int myAge = 25;
int votingAge = 18;
System.out.println(myAge >= votingAge);
An even better approach would be to wrap the code above in an if...else statement, so we can perform
different actions depending on the result:
Example
Output "Old enough to vote!" if myAge is greater than or equal to 18. Otherwise output "Not old enough to vote.":
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
System.out.println("Old enough to vote!");
} else {
System.out.println("Not old enough to vote.");
}
Booleans are the basis for all Java comparisons and conditions.
You will learn more about conditions (if...else) in the next chapter.