Java Reference
In-Depth Information
The output from the program is shown here:
Pay special attention to this line from the program:
Here,
result
is assigned the outcome of the division of 100 by
i
. However, this division
takes place only if
i
is not zero. When
i
is zero, a placeholder value of zero is assigned to
result
.
You don't actually have to assign the value produced by the
?
to some variable. For ex-
ample, you could use the value as an argument in a call to a method. Or, if the expressions
are all of type
boolean
, the
?
can be used as the conditional expression in a loop or
if
state-
ment. For example, here is the preceding program rewritten a bit more efficiently. It pro-
duces the same output as before.