Relational Operators

15.20 Relational Operators

The numerical comparison operators < , > , <= , and >= , and the instanceof operator, are called the relational operators.

RelationalExpression: ShiftExpression RelationalExpression < ShiftExpression RelationalExpression > ShiftExpression RelationalExpression <= ShiftExpression RelationalExpression >= ShiftExpression RelationalExpression instanceof ReferenceType

The relational operators are syntactically left-associative (they group left-to-right).

However, this fact is not useful. For example, a<b<c parses as (a<b)<c , which is always a compile-time error, because the type of a<b is always boolean and < is not an operator on boolean values.

The type of a relational expression is always boolean .

15.20.1 Numerical Comparison Operators < , <= , > , and >= The type of each of the operands of a numerical comparison operator must be a

type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.

Binary numeric promotion is performed on the operands (§5.6.2).

EXPRESSIONS Type Comparison Operator instanceof 15.20.2

Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

If the promoted type of the operands is int or long , then signed integer comparison is performed.

If the promoted type is float or double , then floating-point comparison is performed.

Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.

The result of a floating-point comparison, as determined by the specification of the IEEE 754 standard, is:

• If either operand is NaN, then the result is false. • All values other than NaN are ordered, with negative infinity less than all finite

values, and positive infinity greater than all finite values. • Positive zero and negative zero are considered equal.

For example, -0.0<0.0 is false , but -0.0<=0.0 is true . Note, however, that the methods Math.min and Math.max treat negative zero as being

strictly smaller than positive zero.

Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:

• The value produced by the < operator is true if the value of the left-hand operand is less than the value of the right-hand operand, and otherwise is false .

• The value produced by the <= operator is true if the value of the left-hand operand is less than or equal to the value of the right-hand operand, and otherwise is false .

• The value produced by the > operator is true if the value of the left-hand operand is greater than the value of the right-hand operand, and otherwise is false .

• The value produced by the >= operator is true if the value of the left-hand operand is greater than or equal to the value of the right-hand operand, and otherwise is false .

15.20.2 Type Comparison Operator instanceof The type of the RelationalExpression operand of the instanceof operator must be

a reference type or the null type; otherwise, a compile-time error occurs.

15.21 Equality Operators EXPRESSIONS

It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces

a compile-time error. In such a situation, the result of the instanceof expression could never be true.

At run-time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException . Otherwise the result is false .

Example 15.20.2-1. The instanceof Operator

class Point { int x, y; } class Element { int atomicNumber; } class Test {

public static void main(String[] args) {

Point p = new Point(); Element e = new Element(); if (e instanceof Point) { // compile-time error

System.out.println("I get your point!"); p = (Point)e; // compile-time error

This program results in two compile-time errors. The cast (Point)e is incorrect because no instance of Element or any of its possible subclasses (none are shown here) could possibly

be an instance of any subclass of Point . The instanceof expression is incorrect for exactly the same reason. If, on the other hand, the class Point were a subclass of Element (an admittedly strange notion in this example):

class Point extends Element { int x, y; } then the cast would be possible, though it would require a run-time check, and the

instanceof expression would then be sensible and valid. The cast (Point)e would never raise an exception because it would not be executed if the value of e could not correctly be cast to type Point .