---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 13 Edition"
java_version: 13
chapter: "Chapter 15. Expressions"
section: "15.20"
heading: "15.20. Relational Operators"
parent: "Chapter 15. Expressions"
source: https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.20
id: "15.20"
url: https://jvm.obfuscated.software/jls/se13/15.20.html
raw: https://jvm.obfuscated.software/jls/se13/15.20.md
---

# 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](5.1.8.md)) to a primitive numeric type, or a compile-time error occurs.

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

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

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, or a compile-time error occurs.

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](4.7.md)).

If a cast of the *RelationalExpression* to the *ReferenceType* would be rejected as a compile-time error ([§15.16](15.16.md)), 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 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`.
