Relational or Comparison Operators in Java
In this lesson, we will learn what is the relational or comparison operator and how it works in Java programming with some examples.
What is Relational Operators
Relational operators in Java, also known as comparison operators, are used to compare the values of two operands. For example, they check if one operand is equal to the other, greater than, or less than the other, and so on.
There are 6 types of relational operators in Java and they are:
- == Equality
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
- != Not equal to
Now let's see the examples of all the relational operators one by one for more understanding.
Equality ==
The equality operator == is used to check if the values of two operands are the same. The result of the == operator is 1 (true) if the values are equal; otherwise, the result is 0 (false) if they are not equal.
int x=5, y=5, z=6, a, b;
a = (x == y); // result of a is 1 (true) because x=5 is equal to y=5 so relation is true
b = (x == z); // result of b is 0 (false) because x=5 is not equal to z=6 so relation is false
In the example above, the result of a is 1 because the value of x and y is the same so, the relation is correct. On the other hand, the result of b is 0 because the value of x and z is not the same so the relationship is wrong.
Greater than >
The greater than > relational operator is used to check if the value of the left operand is greater than the value of the right operand. The result of the > operator is 1 (true) if the left operand is greater; otherwise, the result is 0 (false) if it is not.
int x=8, y=5, z=10, a, b;
a = (x > y); // result of a is 1 (true) because x=8 is greater than y=5 so relation is true
b = (x > z); // result of b is 0 (false) because x=8 is not greater than z=10 so relation is false
In the example above, the result of a is 1 because the value of x is greater than the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is not greater than the value of z so the relationship is wrong.
Less than <
The less than < relational operator is used to check if the value of the left operand is less than the value of the right operand. The result of the < operator is 1 (true) if the left operand is less; otherwise, the result is 0 (false) if it is not.
int x=5, y=8, z=3, a, b;
a = (x < y); // result of a is 1 (true) because x=5 is less than y=8 so relation is true
b = (x < z); // result of b is 0 (false) because x=5 is not less than z=3 so relation is false
In the example above, the result of a is 1 because the value of x is less than the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is not less than the value of z so the relationship is wrong.
Greater than or equal to >=
The greater than or equal to >= relational operator is used to check if the value of the left operand is greater than or equal to the value of the right operand. The result of the >= operator is 1 (true) if the condition is met; otherwise, the result is 0 (false) if it is not.
int w=5, x=5, y=3, z=10, a, b, c;
a = (w >= x); // result of a is 1 (true) because w=5 is equal to x=5 so relation is true
b = (x >= y); // result of b is 1 (true) because x=5 is greater than y=3 so relation is true
c = (y >= z); // result of c is 0 (false) because y=5 is neither greater than z=10 nor equal to z=10 so relation is false
In the example above, the result of a is 1 because the value of w is not greater but equal to the value of x so, the relation is correct. Next, the result of b is 1 because the value of x is greater than the value of y so, the relationship is correct. On the other hand, the result of c is 0 because the value of y is neither greater nor equal to the value of z so the relationship is wrong.
Less than or equal to <=
The less than or equal to <= relational operator is used to check if the value of the left operand is less than or equal to the value of the right operand. The result of the <= operator is 1 (true) if the condition is met; otherwise, the result is 0 (false) if it is not.
int w=5, x=5, y=7, z=3, a, b, c;
a = (w <= x); // result of a is 1 (true) because w=5 is equal to x=5 so relation is true
b = (x <= y); // result of b is 1 (true) because x=5 is less than y=7 so relation is true
c = (y <= z); // result of c is 0 (false) because y=7 is neither less than z=3 nor equal to z=3 so relation is false
In the example above, the result of a is 1 because the value of w is not less but equal to the value of x so, the relation is correct. Next, the result of b is 1 because the value of x is less than the value of y so, the relationship is correct. On the other hand, the result of c is 0 because the value of y is neither less nor equal to the value of z so the relationship is wrong.
Not equal to !=
The not equal to != relational operator is used to check if the value of the left operand is not equal to the value of the right operand. The result of the != operator is 1 (true) if the values are not equal; otherwise, the result will be 0 (false) if the values are equal.
int x=5, y=8, z=5, a, b;
a = (x != y); // result of a is 1 (true) because x=5 is not equal to y=8 so relation is true
b = (x != z); // result of b is 0 (false) because x=5 is equal to y=5 so relation is false
In the example above, the result of a is 1 because the value of x is not equal to the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is equal to the value of z so the relationship is wrong.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.