In Java, Relational Operators are useful to check the relation between two operands like we can determine whether two operand values equal or not, etc., based on our requirements.
Generally, the Java relational operators will return true only when the defined operands relationship becomes true. Otherwise, it will return false.
For example, we have integer variables a = 10, b = 20. If we apply a relational operator >= (a >= b), we will get the result false because the variable “a” contains a value that is less than variable b.
The following table lists the different types of operators available in Java relational operators.
Operator | Name | Description | Example (a = 6, b = 3) |
---|---|---|---|
== | Equal to | It compares two operands, and it returns true if both are the same. | a == b (false) |
> | Greater than | It compares whether the left operand greater than the right operand or not and returns true if it is satisfied. | a > b (true) |
< | Less than | It compares whether the left operand less than the right operand or not and returns true if it is satisfied. | a < b (false) |
>= | Greater than or Equal to | It compares whether the left operand greater than or equal to the right operand or not and returns true if it is satisfied. | a >= b (true) |
<= | Less than or Equal to | It compares whether the left operand less than or equal to the right operand or not and returns true if it is satisfied. | a <= b (false) |
!= | Not Equal to | It checks whether two operand values equal or not and return true if values are not equal. | a != b (true) |
Java Relational Operators Example
Following is the example of using the Relational Operators in c# programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static void main(String[] args) { boolean result; int x = 10, y = 20; result = (x == y); System.out.print("Equal to Operator: " + result); result = (x > y); System.out.print("\nGreater than Operator: " + result); result = (x <= y); System.out.print("\nLesser than or Equal to: "+ result); result = (x != y); System.out.print("\nNot Equal to Operator: " + result); System.out.print("\nPress Enter Key to Exit.."); } |
Output: