The Department of Computer Science & Engineering
|
|
STUART C. SHAPIRO: CSE
115 C
|
boolean, and its wrapper class is java.lang.Boolean.true and
false.
The boolean operators are:
| Operator | Meaning |
|---|---|
&& | logical and |
|| | logical or |
! | logical not |
== | equals, or logical equivalence |
!= | not equals, or logical xor |
Truth Tables for logical (boolean) operators:
p q
! p
p && q p || q p == q p != q
true true false true true true false
true false false false true false true
false true true false true false true
false false true false false true false
Relational operators take numbers as arguments, and evaluate to boolean values. The relational operators are:
| Operator | Meaning |
|---|---|
< | less than |
<= | less than or equal to |
== | equals |
!= | not equals |
>= | greater than or equal to |
> | greater than |
The precedence order of operators, including numeric, relational, and boolean is:
| unary +, unary - |
| *, /, % |
| +, - |
| <, <=, >, >= |
| ==, != |
| && |
| || |
The boolean operators use short-circuit evaluation.
Exercise/demo: write lt3(int x, int y, int z) to
return true iff x < y < z.
== vs. equals
getClass() method is useful for seeing whether a variable's
value is a primitive type or a wrapper object.)
5 == 5 --> true
new Integer(5).intValue() == new Integer(5).intValue() --> true
Integer.valueOf(5) == Integer.valueOf(5) -->
true
new Integer(5) == new Integer(5) --> false
but
new Integer(5).equals(new Integer(5)) -->
true
and similarly if you replace Integer with Boolean.