by Dinesh Thakur

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

 

//program Showing Relational Operations
  public class RelationalOperators
{
       public static void main (String[] args)
    {
          int x=5 ,y=3;
          System.out.println("x  > y --> " + (x>y));
          System.out.println("x  < y --> "  + (x<y));
          System.out.println("x <= y --> " + (x<=y));
          System.out.println("x >= y --> " + (x>=y));
          System.out.println("x == y --> " + (x==y));
          System.out.println("x != y --> " + (x!=y));
      
     }
}

Relational Operators in Java Example

Java Example to Perform Relational Operations Using Scanner Class.

import java.util. *;
class RelationalOperationsUsingScanner
{
          public static void main(String args[])
       {
                int x,y;
                Scanner scan = new Scanner(System.in);
                System.out.print("Enter the Value of x : ");
                x=scan.nextInt();
                System.out.print("Enter the Value of y : ");
                y=scan.nextInt();
                System.out.println("x>y : "+(x>y));
                System.out.println("x<y : "+(x<y));
                System.out.println("x>=y : "+( x>=y));
                System.out.println("x<=y : "+( x<=y));
        }
}

Java Example to Perform Relational Operations Using Scanner Class