Relational Operators in Java

In Java, relational operators are used to check the relationship between two operands.

OperatorDescription
==Is Equal To
!=Not Equal To
>Greater Than
>=Greater Than or Equal To
<Less Than
<=Less Than or Equal To

1. “Equal to” Operator(==)

This operator is used to check whether the two given operands are equal or not. The operator returns true if the operand at the left-hand side is equal to the right-hand side, else false.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		boolean result1 = a == b;
		System.out.println("result 1: " + result1);

		int c = 7, d = 7;
		boolean result2 = c == d;
		System.out.println("result 2: " + result2);
	}

}
result 1: false
result 2: true
 

2. “Not Equal to” Operator(!=)

This operator is used to check whether the two given operands are equal or not. It returns true if the operand at the left-hand side is not equal to the right-hand side, else false.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int a = 10, b = 5;
		boolean result1 = a != b;
		System.out.println("result 1: " + result1);

		int c = 7, d = 7;
		boolean result2 = c != d;
		System.out.println("result 2: " + result2);
	}

}
result 1: true
result 2: false
 

3. “Greater than” Operator(>)

This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side.

    package octopuscodes.com.demo;
    
    public class Demo {
    
    	public static void main(String[] args) {
    		int a = 10, b = 5;
    		boolean result1 = a > b;
    		System.out.println("result 1: " + result1);
    
    		int c = 7, d = 17;
    		boolean result2 = c > d;
    		System.out.println("result 2: " + result2);
    	}
    
    }
    result 1: true
    result 2: false
    
     

    4. “Greater than or equal to” Operator(>=)

    This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.

      package octopuscodes.com.demo;
      
      public class Demo {
      
      	public static void main(String[] args) {
      		int a = 10, b = 5;
      		boolean result1 = a >= b;
      		System.out.println("result 1: " + result1);
      
      		int c = 7, d = 7;
      		boolean result2 = c >= d;
      		System.out.println("result 2: " + result2);
      		
      		int e = 7, f = 14;
      		boolean result3 = e >= f;
      		System.out.println("result 3: " + result3);
      	}
      
      }
      result 1: true
      result 2: true
      result 3: false
      
       

      5. “Less than” Operator(<)

      This checks whether the first operand is less than the second operand or not. The operator returns true when the operand at the left-hand side is less than the right-hand side.

      package octopuscodes.com.demo;
      
      public class Demo {
      
      	public static void main(String[] args) {
      		int a = 10, b = 5;
      		boolean result1 = a < b;
      		System.out.println("result 1: " + result1);
      
      		int c = 7, d = 17;
      		boolean result2 = c < d;
      		System.out.println("result 2: " + result2);
      	}
      
      }
      result 1: false
      result 2: true
      
       

      6. "Less than or equal to" Operator(<=)

      This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.

      package octopuscodes.com.demo;
      
      public class Demo {
      
      	public static void main(String[] args) {
      		int a = 10, b = 5;
      		boolean result1 = a <= b;
      		System.out.println("result 1: " + result1);
      
      		int c = 7, d = 7;
      		boolean result2 = c <= d;
      		System.out.println("result 2: " + result2);
      
      		int e = 2, f = 7;
      		boolean result3 = e <= f;
      		System.out.println("result 3: " + result3);
      	}
      
      }
      result 1: false
      result 2: true
      result 3: true