Java Reference
In-Depth Information
Display 3.5
Truth Tables
AND
Exp_1
Exp_2
Exp_1 && Exp_2
true
true
true
true
false
false
false
true
false
NOT
false
false
false
Exp
!(Exp)
true
false
OR
false
true
Exp_1
Exp_2
Exp_1 || Exp_2
true
true
true
true
false
true
false
true
true
false
false
false
A
boolean
variable, that is, one of type
boolean
, can be given the value of a Boolean
expression by using an assignment statement, in the same way that you use an assign-
ment statement to set the value of an
int
variable or any other type of variable. For
example, the following sets the value of the
boolean
variable
isPositive
to
false
:
boolean
variables in
assignments
int
number = -5;
boolean
isPositive;
isPositive = (number > 0);
If you prefer, you can combine the last two lines as follows:
boolean
isPositive = (number > 0);
The parentheses are not needed, but they do make it a bit easier to read.
Once a
boolean
variable has a value, you can use the
boolean
variable just as you
would use any other Boolean expression. For example, the following code
boolean
isPositive = (number > 0);
if
(isPositive)
System.out.println("The number is positive.");
else
System.out.println("The number is negative or zero.");