Java Relational Operators
Java relational operators are used to compare two values and return a boolean value true
or false
. The following are the relational operators in Java:
- Greater than:
>
The greater than operator checks whether the left operand is greater than the right operand.
int a = 10; int b = 5; boolean result = a > b; // result is true
- Less than:
<
The less than operator checks whether the left operand is less than the right operand.
int a = 10; int b = 5; boolean result = a < b; // result is false
- Greater than or equal to:
>=
The greater than or equal to operator checks whether the left operand is greater than or equal to the right operand.
int a = 10; int b = 5; boolean result = a >= b; // result is true
- Less than or equal to:
<=
The less than or equal to operator checks whether the left operand is less than or equal to the right operand.
int a = 10; int b = 5; boolean result = a <= b; // result is false
- Equal to:
==
The equal to operator checks whether the left operand is equal to the right operand.
int a = 10; int b = 10; boolean result = a == b; // result is true
- Not equal to:
!=
The not equal to operator checks whether the left operand is not equal to the right operand.
int a = 10; int b = 5; boolean result = a != b; // result is true