Relational and Logical Operators

Goals

Understand how to use:

Overview

Java's relational and logical operators produce boolean (true or false) expressions:
Relational
Operator
Purpose Form Value
> greater than

expr1 operator expr2

where expr1 and expr2 are integers and/or floating point numbers
true or false
>= greater than or equal to
< less than
<= less than or equal to
== equal to expr1 operator expr2 where expr1 and expr2 are integers and/or floating point numbers
* or *
"object variables" (references) with compatible types (discussed later in the course)
!= not equal to
Logical
Operator
Purpose Form Value
&& and expr1 && expr2 True if both expr1 AND expr2 are true. If expr1 is false, Java doesn't bother to evaluate expr2 (lazy evaluation).
|| or expr1 || expr2 True if either expr1 OR expr2 is true or both are true. If expr1 is true, Java doesn't bother to evaluate expr2 (lazy evaluation).
! not !expr1 True if expr1 is NOT true; otherwise false.

Exercise: Truth Table

Fill in the following "truth table", (available here in a form suitable for printing) where T means true and F means false:
a b a && b a || b ! (a && b) (! a) && b
T T        
T F        
F T        
F F        
Use DrJava to check your answers. For example:
   > boolean a = true;     > boolean b = true;     > a && b     true