Also known as comparison operators, relational operators are another set of operators that are used in java programming language. Similar to arithmetic operators, these operators can be applied to primitive data types only. In this section, we will be discussing relational operators such as ==, which can be used to compare numerical and other data.
Table of Relational Operators
| Operator | Meaning | Returns true if |
|---|---|---|
> |
GREATER THAN | Left operand is greater than right operand. |
>= |
GREATER THAN EQUAL TO | Left operand is greater than or equal to right operand. |
< |
LESS THAN | Left operand is less than right operand. |
<= |
LESS THAN EQUAL TO | Left operand is less than or equal to right operand. |
== |
EQUAL TO | Left operand is equal to right operand . |
!= |
NOT EQUAL TO | Left operand is not equal to right operand. |
DataType for a Relational Operator
A relational operator compares two values and determines the relationship between them. These operators are used to test whether two values are equal or not, whether one value is smaller than another etc. Relational operators return a boolean value which is either true or false.
Example 1:
//greater than
int a = 27, b = 45, c = 45;
System.out.println (" a > b = " + (a > b)); //false
System.out.println (" c > b = " + (c > b)); //false
System.out.println (" b > a = " + (b > a)); //true
In first statement of example one, since a is not greater than b, the value is false. In the second statement, c is not greater tha b hence the answer is false. In the third statement, since b is greater than a, the value b > a is true.
Example 2:
//greater than or equal to
int a = 27, b = 45, c = 45;
System.out.println (" a >= b = " + (a >= b)); //false
System.out.println (" b >= c = " + (b >= a)); //true
System.out.println (" c >= b = " + (c >= b)); //true
In first statement of example one, since a is not greater than or equal to b, the value is false. In the second statement ,even though b is not greater than c, it is equal to c hence we get the result as false. In the third statement, c is equal to b so we get the value as true.
Example 3:
//less than
int a = 27, b = 45, c = 45;
System.out.println (" a < b = " + (a < b)); //true
System.out.println (" b < a = " + (b < a)); //false
System.out.println (" c < b = " + (c < b)); //false
In first statement of example one, since a is less than b, the value is true. In the second statement, even though b is not less than a, so the value is false. In the third statement, c is equal to b and not less than b, so we get the value as false.
Example 4:
//less than or equal to
int a = 27, b = 45, c = 45;
System.out.println (" a <= b = " + (a <= b)); //true
System.out.println (" b <= a = " + (b <= a)); //false
System.out.println (" c <= b = " + (c <= b)); //true
In first statement of example one, even though a is not equal to b, it is less than a, so the answer is true. In the second statement, b is neither less than a nor equal to a, so the value is false. In the third statement, c is equal to b, so we get the value as true.
Example 5:
//equal to
int a = 27, b = 45, c = 45;
System.out.println (" a == b = " + (a == b)); //false
System.out.println (" c == b = " + (c == b)); //true
In first statement of example five, since a is not equal to b, the value is false. In the second statement, b is equal to c so the value is true.
Example 6:
//not equal to
int a = 27, b = 45, c = 45;
System.out.println ("Not equal to...");
System.out.println (" a != b = " + (a != b)); //true
System.out.println (" c != b = " + (c != b)); //false
In first statement of example six, a is not equal to b, so the result is true. In the second statement, b is equal to c so the value is false.



Relational Operators

