The three logical operators of programming are AND, OR, and NOT. These operators are represented by the following symbols in Java:
AND &&
OR || (two vertical bars)
NOT !
The &&
(and) operator requires both operands (values) to be true for the result to be true.
T and T = true
T and F = false
F and T = false
F and F = false
The following are Java examples of using the &&
(and) operator.
( (2 < 3) && (3.5 > 3.0) ) evaluates as true
( (1 == 0) && (2 != 3) ) evaluates as false
The &&
operator performs short-circuit evaluation in Java. If the first half of an &&
statement is false, the operator immediately returns false without evaluating the second half.
The ||
(or) operator requires only one operand (value) to be true for the result to be true.
T or T = true
T or F = true
F or T = true
F or F = false
The following is a Java example of using the ||
(or) operator.
( (2+3 < 10) || (21 > 19) ) evaluates as true
The ||
operator also performs short-circuit evaluation in Java. If the first half of an ||
statement is true, the operator immediately returns true without evaluating the second half.
The !
operator is a unary operator that changes a boolean value to its opposite.
!false = true
!true = false