Theory:Boolean and logical operators
# Boolean type
The
boolean
is a data type that has only two possible values:false
andtrue
. This is also known as the logical type.This type is a common way in programming languages to represent something that has only two opposite states like on or off, yes or no, etc.
If you are writing an application that keeps track of opening and closing a door you'll find it natural to use
boolean
to store current door state.boolean open = true; boolean closed = false; System.out.println(open); // true System.out.println(closed); // false
1
2
3
4
5Important, you cannot assign an integer value to a
boolean
variable. In Java, 0 is not the same asfalse
.# Logical operators
Variables of the
boolean
type are often used to build logical expressions using logical operators. Java has four logical operators NOT, AND, OR and XOR:- NOT is a unary operator that reverses the boolean value. It is denoted as
!
.
boolean f = false; // f is false boolean t = !f; // t is true
1
2- AND is a binary operator that returns
true
if both operands aretrue
, otherwise, it returnsfalse
. It is denoted as&&
.
boolean b1 = false && false; // false boolean b2 = false && true; // false boolean b3 = true && false; // false boolean b4 = true && true; // true
1
2
3
4- OR is a binary operator that returns
true
if at least one operand istrue
, otherwise, it returnsfalse
. It is denoted as||
.
boolean b1 = false || false; // false boolean b2 = false || true; // true boolean b3 = true || false; // true boolean b4 = true || true; // true
1
2
3
4- XOR (exclusive OR) is a binary operator that returns
true
if boolean operands have different values, otherwise, it returnsfalse
. It is denoted as^
.
boolean b1 = false ^ false; // false boolean b2 = false ^ true; // true boolean b3 = true ^ false; // true boolean b4 = true ^ true; // false
1
2
3
4The XOR operator is used less often than others. Just remember that Java has it. If you really need it, you can use it.
# The precedence of logical operators
Below are the logical operations sorted in order of decreasing their priorities in expressions:
!
(NOT),^
(XOR),&&
(AND),||
(OR).So, the following variable is
true
:boolean b = true && !false; // true, because !false is evaluated first
1To change the order of execution you can use round brackets
(...)
.# An example: trekking
As an example, let's write a complex boolean expression that determines the possibility of trekking in summer and in other seasons. Let's suppose that the right conditions for trekking is warm dry weather in summer and autumn.
boolean cold = false; boolean dry = true; boolean summer = false; // suppose now is autumn boolean trekking = dry && (!cold || summer); // true, let's go to trek!
1
2
3
4
5Do not get confused in the expression above, otherwise, you will go trek in bad weather! A programmer should understand not only arithmetic but also logical operations.
# Short-circuiting evaluation
An interesting thing is that the
&&
and||
operators don't evaluate the second argument if it isn't necessary. When the first argument of the&&
operator evaluates tofalse
, the overall value must befalse
; and when the first argument of the||
operator evaluates totrue
, the overall value must betrue
. So:false && ...
->false
, since it is not necessary to know what the right-hand side is;true || ...
->true
, since it is not necessary to know what the right-hand side is.
This behavior is known as short-circuit evaluation (do not confuse it with an electrical short circuit (opens new window)). It reduces the computation time, but can also be used to avoid some errors in programs. We will discuss this in the following topics.
- NOT is a unary operator that reverses the boolean value. It is denoted as