boolean
is a Java primitive data type with exactly 2 possible values: true
and false
(all lowercase, no quotes). Unlike in some other languages, it is NOT possible in Java to use 0
as false
and 1
as true
.
boolean
variables are declared, initialized, and used just like other primitive type variables in Java. boolean
variables are often known as flag variables and are often used to identify whether a particular condition exists.
Example 1
boolean childAteLunch = true;
if(childAteLunch)
System.out.println("Time for nap");
else
System.out.println("Time for lunch");
The variable childAteLunch
is initialized to true
.
The conditional statement if(childAteLunch)
checks if the value of childAteLunch
is true
. Since the value is true
, the code segment prints "Time for nap"
.
Although it would work, it is not necessary to write if(childAteLunch == true)
. The original condition already evaluates to true
if the variable stores true
and false
if the variable stores false
.
Example 2
int grade = (int) (Math.random() * 101);
// 0 <= grade <= 100
boolean earnedHonorRoll = grade >= 90;
if( ! earnedHonorRoll )
System.out.println("Try again next marking period");
else
System.out.println("Congrats");
The variable grade
is initialized to a random value in the range 0 <= grade <= 100
. See Generate random numbers with Math.random() for additional details.
The variable earnedHonorRoll
is initialized to the result of evaluating the boolean
expression grade >= 90
. The expression grade >= 90
evaluates to either true
or false
depending on the value of grade
. The variable earnedHonorRoll
is set to either true
or false
.
The conditional statement if( ! earnedHonorRoll )
checks if the value of earnedHonorRoll
is false
using the Java not operator (!
).
Although it would work, it is not necessary to write if(earnedHonorRoll == false)
. The original condition already evaluates to true
if the variable stores false
and false
if the variable stores true
.
Why avoid == true
and == false
Example with error 1
boolean childAteLunch = false;
if(childAteLunch = true) // mistake
System.out.println("Time for nap"); // prints
else
System.out.println("Time for lunch"); // does not print
System.out.println(childAteLunch); // prints true
This code contains a mistake that is easy to make and easy to miss when reviewing code. The condition is:
if(childAteLunch = true)
instead of:
if(childAteLunch == true)
The single equals sign (=
) is an assignment operator. The code childAteLunch = true
sets the value of childAteLunch
to true
rather than checking if it is true
. When used as a condition, childAteLunch = true
evalutes to true
.
The code segment prints "Time for nap"
instead of "Time for lunch"
. The last print statement prints true
, which is the value of childAteLunch
at the end of the code segment.
Example with error 2
boolean flag = true;
if(flag = false) // mistake
System.out.println("a"); // does not print
else
System.out.println("b"); // prints
System.out.println(flag); // prints false
The code flag = false
sets the value of flag
to false
. When used as a condition, flag = false
evaluates to false
.
The code segment prints "b"
instead of "a"
. The last print statement prints false
, which is the value of flag
at the end of the code segment.