Java Reference
In-Depth Information
Table 4-5 shows the equality, relational, and logical operators used in Java,
including examples of each and the result of evaluating the condition.
Table 4-5
Operator Results in Selection Structures
OPERATOR
MEANING
EXAMPLE
RESULT
TYPE
= =
equal to
2 = = 2
true
equality
1 = = 6
false
!=
not equal to
7 ! = 4
true
equality
4 ! = 4
false
<
less than
3 < 5
true
relational
5 < 3
false
<=
less than or equal to
4 < = 6
true
relational
7 < = 6
false
>
greater than
9 > 7
true
relational
7 > 9
false
>=
greater than or
8 > = 8
true
relational
equal to
8 > = 10
false
&&
logical AND (both
(7 > 3) && (0 < 1)
true
logical
conditions must be
(7 > 3) && (1 < 0)
false
true in order to make
the condition true)
| |
logical OR (one of the
(7 > 3) | | (1 < 0)
true
logical
conditions must be
(3 > 7) | | (1 < 0)
false
true in order to make
the condition true)
!
logical NOT (condition
! (5 = = 4)
true
logical
must evaluate to false
! (a = = a)
false
in order to make the
condition true)
Coding an if Statement to Test the Cancel Button
As shown in the previous steps, when executed, the getSales() method
displays a dialog box prompting the user to enter a dollar amount. The dialog
box contains a text box for user input and two buttons: an OK button and a
Cancel button. If the user clicks the Cancel button, the program should termi-
nate. If the user clicks the OK button, the method validates the user input.
An if statement, as shown in line 33 of Figure 4-13, is used to test if the user
clicked the Cancel button. When clicked, the Cancel button in a JOptionPane
dialog box returns a blank value to the variable, answer. Line 33 uses the equal to
operator (= =) to compare the variable, answer, against a null. In Java, null is a
constant that represents the presence of no data. If the return value matches, or
is equal to, null, the condition is evaluated to be true and the finish() method is
called.
 
Search WWH ::




Custom Search