Java Reference
In-Depth Information
FIGURE 3-16
Occasionally, programmers use casting in order to truncate decimal places.
If, however, a programmer chooses to cast to a smaller data type, he or she then
has to assume responsibility for any loss of precision in the operation.
Comparison Operators
Comparison operators involve two values, as do arithmetic operators;
however, they compare the numbers rather than perform math on them. As
shown in Table 3-8 on the next page, comparison operations include greater
than, less than, equal to, or not equal to, or any combination of those operations.
While the result in arithmetic formulas evaluates to a numeric value, the result
in a comparison operation evaluates to either true or false. Programmers use the
boolean data type to store a comparative result of true or false. As an example,
the statement
boolean isOvertime = ( hours > 40 )
would declare a boolean variable, isOvertime, and store a true value if the vari-
able, hours, is greater than 40. The comparison operation is enclosed in paren-
theses. The identifiers for boolean variables are more easily recognized if a form
of the verb, to be, is used as part of the variable name, as in the example above,
which uses the variable isOvertime.
Table 3-8 lists the six comparison operations, the operator symbols, and
examples of true and false expressions.
Search WWH ::




Custom Search