Logical Operators
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
boolean
Type in Javaboolean
is an atomic/primitive/fundamental type
true
false
&
|
^
!
boolean
Variables in Java
// Declare a boolean variable named ok boolean ok; // Assign the value true to ok ok = true;
true & true
true | false
!false
true | (false & true)
enrolled & paid
student | professor
!old
&&
||
false && x
evaluates to false
(sometimes called the simplification rule) so
x
isn't evaluatedtrue || x
evaluates to true
(sometimes called the addition rule)
so x
isn't evaluated(true) || (false && true)
(false) || (false && true)
(true) && (false && true)
(false) && (false && true)
(true && true) || (false && true)
(false && true) || (false && true)
(true || true) && (false && true)
(false || true) && (false && true)
^^
operator?