Relational operators in java

Let’s learn relational operators in java.

Relational operators in java

Relational operators are used to compare two operands or variables for equality, greater than, less than etc., and return boolean output. There are six relational operators,

  • equals to operator (==)
  • less than operator (<)
  • greater than operator (>)
  • not equals to operator (!=)
  • greater than equals to operator (>=)
  • less than equal to operator (<=)

The output of relational operator is boolean value, that is, true or false.

equals to operator (==): compares values between two operands for their equality. If both operands value have equal values it returns true else returns false.

Let’s see an example for equals to (==) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c == c);
      // comparing character to integer
      System.out.println(c == a);
      // comparing ASCII value to integer
      System.out.println(71 == a);
      // comparing integer to double
      System.out.println(a == d);
      // comparing integer to integer
      System.out.println(a == a);
   }
}


Output:

true
false
false
false
true


less than operator (<): compares values between two operands for less than value. If operand value on left is less than the operand value on the right it returns true else returns false.

Let’s see an example for less than (<) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c < c);
      // comparing character to integer
      System.out.println(c < a);
      // comparing ASCII value to integer
      System.out.println(71 < a);
      // comparing integer to double
      System.out.println(a < d);
      // comparing integer to integer
      System.out.println(a < a);
   }
}


Output:

false
false
false
true
false


greater than operator (>): compares values between two operands for greater than value. If operand value on left is greater than operand value on the right it returns true else returns false.

Let’s see an example for greater than (>) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c > c);
      // comparing character to integer
      System.out.println(c > a);
      // comparing ASCII value to integer
      System.out.println(71 > a);
      // comparing integer to double
      System.out.println(a > d);
      // comparing integer to integer
      System.out.println(a > a);
   }
}


Output:

false
true
true
false
false


not equals to operator (!=): compares values between two operands for their inequality. If operands value have unequal values then it returns true else returns false.

Let’s see an example for not equals to (!=) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c != c);
      // comparing character to integer
      System.out.println(c != a);
      // comparing ASCII value to integer
      System.out.println(71 != a);
      // comparing integer to double
      System.out.println(a != d);
      // comparing integer to integer
      System.out.println(a != a);
   }
}


Output:

false
true
true
true
false


greater than equals to operator (>=):

compares values between two operands and returns true if either of below conditions are true

  • if operand on left of greater than equals to operator is greater than operand on right, OR
  • if operand on left of greater than equals to operator is equal to operand on its right.

Let’s see an example for greater than equals to (>=) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c >= c);
      // comparing character to integer
      System.out.println(c >= a);
      // comparing ASCII value to integer
      System.out.println(71 >= a);
      // comparing integer to double
      System.out.println(a >= d);
      // comparing integer to integer
      System.out.println(a >= a);
   }
}


Output:

true
true
true
false
true


less than equals to operator (<=): compares values between two operands and returns true if either of below conditions are true

  • if operand on left of less than equals to operator is smaller than operand on right, OR
  • if operand on left of less than equals to operator is equal to operand on its right.

Let’s see an example for less than equals to (<=) relational operator in java.

public class RelationalOperatorExample
{
   public static void main(String[] args) 
   {
      int a = 7;
      double d = 262.0001;
      char c = 'm';
      // comparing character to character
      System.out.println(c <= c);
      // comparing character to integer
      System.out.println(c <= a);
      // comparing ASCII value to integer
      System.out.println(71 <= a);
      // comparing integer to double
      System.out.println(a <= d);
      // comparing integer to integer
      System.out.println(a <= a);
   }
}


Output:

true
false
false
true
true