Boolean data type
The Boolean data type represents 0 (false) or 1 (true). The computer is able to test the inequality or equality true or false.
Using Boolean Expressions: Predicate Method
A predicate method returns a boolean value
public boolean isOverdrawn()
{
return balance < 0;
}
Use in conditions
if (harrysChecking.isOverdrawn())
Useful predicate methods in Character class:
isDigit
isLetter
isUpperCase
isLowerCase
if (Character.isUpperCase(ch)) . . .
Useful predicate
methods in
Scanner
class:
hasNextInt() and hasNextDouble()
if (in.hasNextInt()) n = in.nextInt();
AND & OR & NOT
AND (&&):
If there are two events joined by AND, both events have to be true
for the result of join to be true.
For example, if x is between 100 and 200, you can write
if (x >100 && x <200) ...
OR (
|| )
If there are two events joined by OR, both events have to be false
for the result of join to be false.
For example, if input can be 's' or 'S', you can write
if (input == 's' || input == 'S')
...
NOT (
!)
Opposite of the original.

|
A |
B |
A && B |
|
true |
true |
true |
|
true |
false |
false |
|
false |
Any |
false |
|
A |
B |
A || B |
|
true |
Any |
true |
|
false |
true |
true |
|
false |
false |
false |
|
A |
! A |
|
true |
false |
|
false |
true |
Using Boolean Variables
private boolean married;
Set to truth value:
married = input.equals("M");
Use in conditions:
if (married) . . . else . . . if (!married) . . .
It is considered gauche to write a test such as
if (married == true) . . . // Don't
if (character.isDigit(ch) == false) . . . // Don'tJust use the simpler test
if (married) . . .
if (!Character. isDigit(ch)) . . .