Incremental Java
Relational Operators

Relational Operators

Equality (and inequality) operators work with all types. Relational operators only work with some types. In particular, it works with int and double, i.e., numeric types.

Relational operators, like equality operators, return a boolean value. These operators are used to tell you when an expression is greater than, less than, or equal to another expression.

Operators

> is the greater-than operator.

>= is the greater-than-or-equal operator.

< is the less-than operator.

<= is the less-than-or-equal operator.

Negation

If x > y evaluates to false, then x <= y evaluates to true. If x > y evaluates to true, then x <= y evaluates to false.

The negation greater-than is less-than-or-equal. Some people who've never heard of that find it hard to believe. They think "No way. The opposite of greater than is less than!"

But that's not true. Let's see why. Suppose I say "I am older than you". This means "My age is greater than your age". When would that be false? That is, when would I be lying? Let's assume that ages are integer values.

Certainly, I'm lying if I'm really younger than you. But what if you and I are the same age? In that case, I'm still lying, because if we're the same age, then I can't be older than you. So I'm lying if my age is less than or equal to your age.

We can summarize the negation of these relational operators (plus the equality operators in the following chart.

Operator Negation
exprleft == exprright exprleft != exprright
exprleft != exprright exprleft == exprright
exprleft > exprright exprleft <= exprright
exprleft >= exprright exprleft < exprright
exprleft < exprright exprleft >= exprright
exprleft <= exprright exprleft > exprright

Chart

This chart summarizes the behavior of the four relational operators.

Subevaluations Result
EVAL[ exprleft ] > EVAL[ exprright ] EVAL[ exprleft > exprright ] = true
EVAL[ exprleft ] <= EVAL[ exprright ] EVAL[ exprleft > exprright ] = false
EVAL[ exprleft ] >= EVAL[ exprright ] EVAL[ exprleft >= exprright ] = true
EVAL[ exprleft ] < EVAL[ exprright ] EVAL[ exprleft >= exprright ] = false
EVAL[ exprleft ] < EVAL[ exprright ] EVAL[ exprleft < exprright ] = true
EVAL[ exprleft ] >= EVAL[ exprright ] EVAL[ exprleft < exprright ] = false
EVAL[ exprleft ] <= EVAL[ exprright ] EVAL[ exprleft <= exprright ] = true
EVAL[ exprleft ] > EVAL[ exprright ] EVAL[ exprleft <= exprright ] = false

The right column says what the evaluation's result is based on the evaluation of the left and right operand from the left column.