0% completed
Relational operators are used in Java to compare two values. These operators evaluate expressions and return a boolean value (true
or false
) based on the relationship between operands. They are fundamental in decision-making statements such as if
and loops.
Operator | Description | Example |
---|---|---|
== | Equal to: checks if two values are equal | a == b |
!= | Not equal to: checks if two values are not equal | a != b |
> | Greater than: checks if the left operand is greater than the right | a > b |
< | Less than: checks if the left operand is less than the right | a < b |
>= | Greater than or equal to: checks if the left operand is greater than or equal to the right | a >= b |
<= | Less than or equal to: checks if the left operand is less than or equal to the right | a <= b |
Below is an example that demonstrates relational operators by comparing two integer values.
Explanation:
a
and b
, using all the relational operators.These examples show how relational operators can be used to compare values and make decisions based on those comparisons in your Java programs.
.....
.....
.....