Showing posts with label Comparison Operators (Relational Operators). Show all posts
Showing posts with label Comparison Operators (Relational Operators). Show all posts

Thursday, 28 May 2015

Java Comparison Operators (Relational Operators)

The Java supports the following comparison operators:

<, <=, >, >=,  = =, != and instanceof operator

These operators can be classified into three categories:
  1. Ordinal Comparisons or Relational Operators
  2. Equality Comparisons or Equality Operators
  3. instanaceof  Operator

Ordinal Comparisons or Relational Operators

Java Supports the following ordinal comparisons/relational operators:

<, <=, >, >=

These operators are used to compare ordinal data types i.e. data types where values have numeric order.

These operators cannot be applied on boolean types and reference types as these types do not have any numeric ordering among the values.

Equality Comparisons or Equality Operators

Equality comparisons test whether two values are same. They can be applied on all Java types including boolean and reference data types.

There are certain restrictions when these operators are used to compare reference data types.

Only references of the objects, whose classes belong to common type hierarchy, can be compared.

instanaceof  Operator


The instanceof operator is used to test the class of an object. The instanceof operator has the

general form:

object   instanceof   type

Here, object is an instance of a class, and type is a class type. If object is an instance of the

specified type or instance of any sub-class of the specified type, then the instanceof operator returns true. Otherwise its result is false.