Java Reference
In-Depth Information
Relational Operators
To make decisions, you must be able to express conditions and make comparisons. For
example, the interest rate paid and service charges imposed on a checking account might
depend on the balance at the end of the month. If the balance is less than some minimum
balance, not only is the interest rate lower, but there is also usually a service charge.
Therefore, to determine the interest rate, you must be able to state the minimum balance
(a condition) and compare the account balance with the minimum balance. The premium
on an insurance policy is also determined by determining conditions and making
comparisons. For example, to determine an insurance premium, you must be able to check
the smoking behavior of the policyholder. Nonsmokers (the condition) receive lower
premiums than smokers. Both of these examples involve comparing items. Items can be
compared in various ways. For example, you can compare items for equality or inequality.
You can also determine whether one item is greater than another item, and so on.
An expression that has a value of either true or false is called a logical (boolean)
expression.Thevalues true and false are called logical (boolean) values. In Java, a
condition is represented by a logical (boolean) expression; conditions are either true or false .
Logical (boolean) expression: An expression that has a value of either true or false .
Suppose i and j are integers. Consider the expression:
i > j
This is a logical expression. It will have the value true if the value of i is greater than the
value of j ; otherwise, it will have the value false . The symbol > is called a relational
operator because the value of i > j is true only when the relationship ''greater than''
holds for i relative to j .
Relational operator: An operator that allows you to make comparisons in a program.
Java includes six relational operators that enable you to make comparisons. Table 4-1 lists
the relational operators.
TABLE 4-1 Relational Operators in Java
Operator
Description
==
equal to
!=
not equal to
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
 
Search WWH ::




Custom Search