This chapter goes into more depth about relational and logical operators. You will have to use these concepts to write complex programs that other people can read and follow.
3. Numeric Data and Operators
3.5. Relational Operators
There are several relational operations that can be performed on integers: <, >, <=, >=, ==, and !=. These correspond to the algebraic operators <, >, , , =, and 6=. Each of these operators takes two operands (integer or real) and returns a boolean result. They are defined in Table 5.9.
Table 5.9 Relational operators
Operator | Operation | Java Expression |
---|---|---|
< | Less than | 5<10 |
> | Greater than | 10>5 |
<= | Less than or equal to | 5<=10 |
>= | Greater than or equal to | 10>=5 |
== | Equal to | 5==5 |
!= | Not equal to | 5 ! = 4 |
Note that several of these relational operators require two symbols in Java. Thus, the familiar equals sign (=) is replaced in Java by ==. This is so the equality operator can be distinguished from the assignment operator.
Also, less than or equal to (<=), greater than or equal to (>=), and not equal to (!=) require two symbols, instead of the familiar , , and 6= from algebra. In each case, the two symbols should be consecutive. It is an error in Java for a space to appear between the < and = in <=.
JAVA DEBUGGING TIP Equality and Assignment. A common semantic error among beginning programmers is to use the assignment operator (=) when the equality operator (==) is intended. |
---|
Among the relational operators, the inequalities (<, >, <=, and >=) have higher precedence than the equality operators (== and !=). In an expression that involves both kinds of operators, the inequalities would be evaluated first. Otherwise,
the expression is evaluated from left to right.
Taken as a group the relational operators have lower precedence than the arithmetic operators. Therefore, in evaluating an expression that involves both arithmetic and relational operators, the arithmetic operations are done first. Table 5.10 includes
all of the numeric operators introduced so far.
Table 5.10 Numeric operator precedence including relations
Precedence Order |
Operator | Operation | ||
1 | () | Parentheses | ||
2 | ++ | -- | Increment, decrement | |
3 | * / | % | Multiplication, division, modulus | |
4 | + — | Addition, subtraction | ||
5 | < > | <= | >= | Relational operators |
6 | Equality operators |
To take an example, let us evaluate the following complex expression:
9 + 6 <= 25 * 4 + 2
To clarify the implicit operator precedence, we first parenthesize the expression
(9 + 6) <= ((25 * 4) + 2)
and then evaluate it step by step:
Step 1. |
(9+6) <= ((25 * 4) +2) |
---|---|
Step 2. | (9+6) <= (100+2) |
Step 3. | 15 <= 102 |
Step 4. | true |
The following expression is an example of an ill-formed expression:
9+6 <= 25 * 4 == 2
That the expression is ill formed becomes obvious if we parenthesize it and then attempt to evaluate it:
Step 1. |
((9+6) <= (25 * 4)) == 2 |
---|---|
Step 2. | (15 <= 100) == 2 |
Step 3. | true == 2 // Syntax error results here |
The problem here is that the expression true == 2 is an attempt to compare an int and a boolean value, which can't be done. As with any other Strong typing binary operator, the == operator requires that both of its operands be of the same type. This is another example of Java's strong type checking