Java From Beginner To Advanced

0% completed

Previous
Next
Relational operators

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.

Relational Operators

OperatorDescriptionExample
==Equal to: checks if two values are equala == b
!=Not equal to: checks if two values are not equala != b
>Greater than: checks if the left operand is greater than the righta > b
<Less than: checks if the left operand is less than the righta < b
>=Greater than or equal to: checks if the left operand is greater than or equal to the righta >= b
<=Less than or equal to: checks if the left operand is less than or equal to the righta <= b

Example 1: Comparing Integer Values

Below is an example that demonstrates relational operators by comparing two integer values.

Java
Java

. . . .

Explanation:

  • The code compares two integer values, a and b, using all the relational operators.
  • Each comparison is evaluated and the boolean result is printed.

These examples show how relational operators can be used to compare values and make decisions based on those comparisons in your Java programs.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next