Relational Operators in Java

Java provides six relational operators. They are all binary operators and determine the relationship between the two operands.


For example, assume X and Y are two variables with values 7 and 10 respectively.
OperatorOperator NameTypeExample
==Equal tobinaryX == Y returns false
!=Not Equal tobinaryX != Y returns true
>Greater thanbinaryX > Y returns false
>=Greater than or Equal tobinaryX >= Y returns false
<Less thanbinaryX < Y returns true
<=Less than or Equal tobinaryX <= Y returns true

Two points worth noting about relational operators are:

  1. Operators ==, != can be used to compare any type of data including integers, floating point numbers, characters and booleans.
  2. Operators >, >=, <, <= can be used to compare only numeric types.

Also note that the result of a relational operation is a boolean value true or false.


An Example:

It produces the output: