Let’s learn what are the 6 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