Relational Operators in Java


Relational Operators are a bunch of binary operators that are used to check for relations between two operands including equality, greater than, less than, etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on.Relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Operatoruses
== equality operator
!= non-equality operator
< less than operator
> greater than operator
<= less than or equal to operator
>= greater than or equal to operator

This is a Java program that demonstrates the use of relational operators to compare two integer variables a and b. The program initializes the variables a and b with the values 100 and 50, respectively. It then uses six different relational operators to compare the values of a and b, and prints the result of each comparison to the console using the System.out.println() method.

  • The first line (Equal to : false) uses the == operator to check if a is equal to b. Since a is not equal to b, the expression (a==b) evaluates to false.
  • The second line (Not Equal to : true) uses the != operator to check if a is not equal to b. Since a is not equal to b, the expression (a!=b) evaluates to true.
  • The third line (Greater than : true) uses the > operator to check if a is greater than b. Since a is greater than b, the expression (a>b) evaluates to true.
  • The fourth line (Less than : false) uses the < operator to check if a is less than b. Since a is not less than b, the expression (a<b) evaluates to false.
  • The fifth line (Greater than or equal to : true) uses the >= operator to check if a is greater than or equal to b. Since a is greater than b, the expression (a>=b) evaluates to true.
  • The sixth line (Less than or equal to : false) uses the <= operator to check if a is less than or equal to b. Since a is not less than or equal to b, the expression (a<=b) evaluates to false.

Source Code

public class relational {
    public static void main(String args[])
    {
        int a=100,b=50;
        System.out.println("Equal to : "+(a==b));
        System.out.println("Not Equal to : "+(a!=b));
        System.out.println("Greater than : "+(a>b));
        System.out.println("Less than : "+(a<b));
        System.out.println("Greater than or equal to : "+(a>=b));
        System.out.println("Less than or equal to : "+(a<=b));
    }
}
 

Output

Equal to : false
Not Equal to : true
Greater than : true
Less than : false
Greater than or equal to : true
Less than or equal to : false
To download raw file Click Here

Basic Programs