Booleans #
What is Boolean? #
Boolean is a data type that can have only two opposite values like
- on or off
- yes or no
- true or false
In Java the boolean
type can store true
or false
values.
Boolean Variables #
A boolean variable is declared with boolean
type and then either true
or false
is assigned to that variable.
boolean isDoorOpen = true;
boolean isCameraOpen = false;
System.out.println(isDoorOpen); // outputs true
System.out.println(isCameraOpen); // outputs false
Boolean Expressions #
Boolean expressions are evaluations that produce a boolean result, such as comparision.
For example if we need to control if someone has a right to vote we can use comparision with an if
statement.
int ageOfVoter = 21
if (ageOfVoter >= 18) {
System.out.println("Has right to vote.");
}
else {
System.out.println("Does not have right to vote!");
}