Java – Relational Operator

Comparison operators are used to comparing two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values.

They are used with a loop statement and also with if-else statements.

Following are the Relational operators in Java:

OperatorNameExample
==Equal to.A == B
!=Not equal.A != B
>Greater than.A > B
<Less than.A < B
>=Greater than or equal to.A >= B
<=Less than or equal to.A <= B

Java Program for Relational Operator

  public class Relational 
  {

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

    System.out.println("Is x == y = " + (x == y) );
    System.out.println("Is x != y = " + (x != y) );
    System.out.println("Is x > y = " + (x > y) );
    System.out.println("Is x < y = " + (x < y) );
    System.out.println("Is y >= a = " + (y >= x) );
    System.out.println("Is y <= a = " + (y <= x) );
    
    //Demonstration to use it with if-else 
    if(x==y){
        System.out.println("X is equal to y"); 
    }
    else{
        System.out.println("X is not equal to y" );
    }
    
   }
  }

Output:

  Is x == y = false
  Is x != y = true
  Is x > y = false
  Is x < y = true
  Is y >= a = true
  Is y <= a = false
  X is not equal to y