3.9 Relational Operators: <, <=, >, >=

Given that a and b represent numeric expressions, the relational (also called comparison) operators are defined as shown in Table 3.6.

Table 3.6. Relational Operators

a < b

a less than b?

a <= b

a less than or equal to b?

a > b

a greater than b?

a >= b

a greater than or equal to b?

All relational operators are binary operators, and their operands are numeric expressions. Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators.

double  hours = 45.5;
boolean overtime = hours >= 35.0;    // true.
boolean order = 'A' < 'a';           // true. Binary numeric promotion applied.

Relational operators are nonassociative. Mathematical expressions like a b c must be written using relational and boolean logical/conditional operators.

int a = 1, b = 7, c = 10;
boolean valid1 = a <= b <= c;             // (1) Illegal.
boolean valid2 = a <= b && b <= c;        // (2) OK.

Since relational operators have left associativity, the evaluation of the expression a <= b <= c at (1) in the examples above would proceed as follows: ((a <= b) <= c). Evaluation of (a <= b) would yield a boolean value that is not permitted as an operand of a relational operator, that is, (<boolean value> <= c) would be illegal.