Java Relational Operators


Relational operators in Java are used to compare two operands and determine their relationship. These operators return a boolean value (true or false) based on the comparison. Here’s a detailed explanation of relational operators along with examples.

List of Relational Operators in Java

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

1. Equal to Operator (==)

The equal to operator checks if two operands are equal.

Example:

public class EqualToExample { public static void main(String[] args) { int a = 10; int b = 10; boolean isEqual = (a == b); // Check if a is equal to b System.out.println("Is a equal to b? " + isEqual); // Output: Is a equal to b? true } }

2. Not Equal to Operator (!=)

The not equal to operator checks if two operands are not equal.

Example:

public class NotEqualToExample { public static void main(String[] args) { int a = 10; int b = 5; boolean isNotEqual = (a != b); // Check if a is not equal to b System.out.println("Is a not equal to b? " + isNotEqual); // Output: Is a not equal to b? true } }

3. Greater Than Operator (>)

The greater than operator checks if the left operand is greater than the right operand.

Example:

public class GreaterThanExample { public static void main(String[] args) { int a = 10; int b = 5; boolean isGreater = (a > b); // Check if a is greater than b System.out.println("Is a greater than b? " + isGreater); // Output: Is a greater than b? true } }

4. Less Than Operator (<)

The less than operator checks if the left operand is less than the right operand.

Example:

public class LessThanExample { public static void main(String[] args) { int a = 5; int b = 10; boolean isLess = (a < b); // Check if a is less than b System.out.println("Is a less than b? " + isLess); // Output: Is a less than b? true } }

5. Greater Than or Equal To Operator (>=)

The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.

Example:

public class GreaterThanOrEqualToExample { public static void main(String[] args) { int a = 10; int b = 10; boolean isGreaterOrEqual = (a >= b); // Check if a is greater than or equal to b System.out.println("Is a greater than or equal to b? " + isGreaterOrEqual); // Output: Is a greater than or equal to b? true } }

6. Less Than or Equal To Operator (<=)

The less than or equal to operator checks if the left operand is less than or equal to the right operand.

Example:

public class LessThanOrEqualToExample { public static void main(String[] args) { int a = 5; int b = 10; boolean isLessOrEqual = (a <= b); // Check if a is less than or equal to b System.out.println("Is a less than or equal to b? " + isLessOrEqual); // Output: Is a less than or equal to b? true } }

Summary

Relational operators in Java are essential for comparing values and making decisions based on those comparisons. They are commonly used in control flow statements like if, for, and while loops. Understanding how to use these operators effectively allows you to implement logic that responds to varying conditions in your programs.