Explain about relational operators with an example.


 Relational operators also known as comparison operators are used to check the relation between two operands. In other words, the operators used to make comparisons between two operands are called relational operators. Relational operators are binary operators and hence require two operands. The result of a relational operation is a Boolean value that can only be true or false according to the result of the comparison.

Relational operators are normally used with if statements, while loop, do-while loop and for loop to make branching and looping decisions.

 

Table shows you the different relational operators used in java programming.

 

                                                          Relational Operators

 

Operator

Operations

Example

<=

>=

==

!=

Less than

Greater than

Less than or equal to

Greater than equal to

Equal to

Not equal to

x<y

x>y

x<=y

x>=y

x=y

x!=y

 

Example

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;

      System.out.println("a == b = " + (a == b) );
      System.out.println("a != b = " + (a != b) );
      System.out.println("a > b = " + (a > b) );
      System.out.println("a < b = " + (a < b) );
      System.out.println("b >= a = " + (b >= a) );
      System.out.println("b <= a = " + (b <= a) );
   }
}

This will produce the following result −

Output

a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false