The relational comparison operators in Java are used for less than (<), less than or equal to (<=), greater than or equal to (>=), greater than (>), and instanceof comparison operations. They may appear in a relational expression:
The relational comparison operators are equal in precedence and are evaluated from left to right. The <, <=, >=, and > operators are numerical comparison operators, while instanceof is a type comparison operator. All of these operators produce boolean values.
References Shift Operators; Order of Operations; Type 3
The less-than operator < performs a comparison between its operands and returns a boolean value. It returns the pure value true if its left operand is less than its right operand; otherwise the operator returns the pure value false. The < operator may appear as part of a relational expression. The less-than operator never throws an exception.
The types of both operands of the less-than operator must be arithmetic types, or a compile-time error occurs. The < operator may perform type conversions on its operands:
The comparison of any two arithmetic values produces true if the value of the left operand is less than the value of the right operand; otherwise the comparison produces false. The comparison of floating-point data is governed by the following additional rules:
References Arithmetic Types
The less-than-or-equal-to operator <= performs a comparison between its operands and returns a boolean value. It returns the pure value true if its left operand is less than or equal to its right operand; otherwise the operator returns the pure value false. The <= operator may appear as part of a relational expression. The less-than-or-equal-to operator never throws an exception.
The types of both operands of the less-than-or-equal-to operator must be arithmetic types, or a compile-time error occurs. The <= operator may perform type conversions on its operands:
The comparison of any two arithmetic values produces true if the value of the left operand is less than or equal to the value of the right operand; otherwise the comparison produces false. The comparison of floating-point data is governed by the following additional rules:
References Arithmetic Types
The greater-than-or-equal-to operator >= performs a comparison between its operands and returns a boolean value. It returns the pure value true if its left operand is greater than or equal to its right operand; otherwise the operator returns the pure value false. The >= operator may appear as part of a relational expression. The greater-than-or-equal-to operator never throws an exception.
The types of both operands of the greater-than-or-equal-to operator must be arithmetic types, or a compile-time error occurs. The >= operator may perform type conversions on its operands:
The comparison of any two arithmetic values produces true if the value of the left operand is greater than or equal to the value of the right operand; otherwise the comparison produces false. The comparison of floating-point data is governed by the following additional rules:
References Arithmetic Types
The greater-than operator > performs a comparison between its operands and returns a boolean value. It returns the pure value true if its left operand is greater than its right operand; otherwise the operator returns the pure value false. The > operator may appear as part of a relational expression. The greater-than operator never throws an exception.
The types of both operands of the greater-than operator must be arithmetic types, or a compile-time error occurs. The > operator may perform type conversions on its operands:
The comparison of any two arithmetic values produces true if the value of the left operand is greater than the value of the right operand; otherwise the comparison produces false. The comparison of floating-point data is governed by the following additional rules:
References Arithmetic Types
The instanceof operator performs a type comparison between its operands and returns a boolean value. It returns the pure value true if the object referred to by the left operand can be cast to the type specified as the right operand; otherwise the operator returns the pure value false. If the value of the left operand is null, the instanceof operator returns the pure value false. The instanceof operator may appear as part of a relational expression. The instanceof operator never throws an exception.
The type of the left operand of the instanceof operator must be a reference type, or a compile-time error occurs.
All objects inherit a method called equals() from the Object class. The equals() method defined in the Object class returns true if the two objects being compared are the same object. For some classes, it is more appropriate to override the equals() method so that it compares the contents of two objects. Before such a method can do the comparison, it should verify that the objects are instances of the same class by using instanceof. For example, let's suppose that you are defining a class to represent complex numbers. Since you want the equals() method to compare the contents of complex number objects, you define an equals method for the complex number class that looks like this:
boolean equals (Object o) { if (o instanceof complexNumber) return o.real == this.real && o.imaginary == this.imaginary; }
The instanceof operator can also be used to find out if an object is an instance of a class that implements an interface. For example:
if (o instanceof Runnable) (new Thread((Runnable)o).start;
References Casts; Class Types; Interface Types