Java Reference
In-Depth Information
This basic control flow statement will evaluate
condition
, and if
true
, the first
block of code will execute; if
false
, the second block will be performed. As is the
case in COBOL, the second code block (the
else
condition) is optional.
if (inputMsg.equals ("Any Text")) {
...
}
else {
...
}
condition
can be any Java expression that returns a boolean. The result of that ex-
pression is evaluated.
if (myErrorMsg.msgText.equals ("Any Text")) {
...
}
condition
must be a
boolean
expression, that is, one that evaluates to either true or
false. In the example, the
equals()
method returns a true or false boolean, so it is a
good candidate for inclusion in an
if
statement.
The expression
myErrorMsg.msgText.equals
contains two member operators (that
is, two periods). This implies that two members (a data member and a method
member in this case) will be accessed.
Evaluate the expression this way: The data member
msgText
in the object
myErrorMsg
is a
String
variable. As a result, it contains a method named
equals()
. This method
accepts a
String
parameter and returns a boolean result of true if the passed
String
parameter contains the same text as is contained in this
String
. The boolean result is
evaluated by the
if
operator.
Multiple expressions can be grouped, and the result of such grouping will be eval-
uated. The logical operators AND (&&), OR(||), and NOT (!) group boolean expres-
sions in the same way their text equivalents do in COBOL. For example, this statement:
if (myErrorMsg.msgText.equals ("Any Text") ||
(myErrorMsg.msgText.equals ("Some Text")) {
... // The IF code block
}