Introduction
Relational operators in Java are used to compare two values or variables. These operators return a boolean value (true
or false
) based on whether the specified condition is met. Relational operators are commonly used in control flow statements, such as if
statements, loops, and conditional expressions, to make decisions in code.
Key Points:
- Comparison: Relational operators compare two operands.
- Boolean Result: The result of a relational operation is always
true
orfalse
. - Use in Control Flow: Often used in conditions for
if
,while
, andfor
loops.
List of Relational Operators
Java provides the following relational operators:
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | a == b | true if a equals b |
!= | Not equal to | a != b | true if a does not equal b |
> | Greater than | a > b | true if a is greater than b |
< | Less than | a < b | true if a is less than b |
>= | Greater than or equal to | a >= b | true if a is greater than or equal to b |
<= | Less than or equal to | a <= b | true if a is less than or equal to b |
Detailed Examples of Relational Operators
Let’s explore each relational operator with examples.
1. Equal to (==)
The ==
operator checks if two operands are equal.
public class EqualToExample { public static void main(String[] args) { int a = 5; int b = 5; boolean result = (a == b); // true, since a equals b System.out.println("a == b: " + result); // Output: a == b: true } }
Explanation:
- Comparison: The values of
a
andb
are compared, and since they are equal, the result istrue
.
Output:
a == b: true
2. Not Equal to (!=)
The !=
operator checks if two operands are not equal.
public class NotEqualToExample { public static void main(String[] args) { int a = 5; int b = 3; boolean result = (a != b); // true, since a does not equal b System.out.println("a != b: " + result); // Output: a != b: true } }
Explanation:
- Comparison: The values of
a
andb
are compared, and since they are not equal, the result istrue
.
Output:
a != b: true
3. Greater than (>)
The >
operator checks if the left-hand operand is greater than the right-hand operand.
public class GreaterThanExample { public static void main(String[] args) { int a = 7; int b = 5; boolean result = (a > b); // true, since a is greater than b System.out.println("a > b: " + result); // Output: a > b: true } }
Explanation:
- Comparison: The value of
a
is compared withb
, and sincea
is greater thanb
, the result istrue
.
Output:
a > b: true
4. Less than (<)
The <
operator checks if the left-hand operand is less than the right-hand operand.
public class LessThanExample { public static void main(String[] args) { int a = 3; int b = 5; boolean result = (a < b); // true, since a is less than b System.out.println("a < b: " + result); // Output: a < b: true } }
Explanation:
- Comparison: The value of
a
is compared withb
, and sincea
is less thanb
, the result istrue
.
Output:
a < b: true
5. Greater than or Equal to (>=)
The >=
operator checks if the left-hand operand is greater than or equal to the right-hand operand.
public class GreaterThanOrEqualToExample { public static void main(String[] args) { int a = 5; int b = 5; boolean result = (a >= b); // true, since a is equal to b System.out.println("a >= b: " + result); // Output: a >= b: true } }
Explanation:
- Comparison: The value of
a
is compared withb
, and sincea
is equal tob
, the result istrue
.
Output:
a >= b: true
6. Less than or Equal to (<=)
The <=
operator checks if the left-hand operand is less than or equal to the right-hand operand.
public class LessThanOrEqualToExample { public static void main(String[] args) { int a = 3; int b = 5; boolean result = (a <= b); // true, since a is less than b System.out.println("a <= b: " + result); // Output: a <= b: true } }
Explanation:
- Comparison: The value of
a
is compared withb
, and sincea
is less thanb
, the result istrue
.
Output:
a <= b: true
Conclusion
Java’s relational operators are essential for making decisions in your code by comparing values. These operators are frequently used in control flow statements, such as if
conditions and loops, to determine the flow of execution based on comparisons.
Summary:
- Equal to (
==
): Checks if two values are equal. - Not Equal to (
!=
): Checks if two values are not equal. - Greater than (
>
): Checks if the left-hand value is greater than the right-hand value. - Less than (
<
): Checks if the left-hand value is less than the right-hand value. - Greater than or Equal to (
>=
): Checks if the left-hand value is greater than or equal to the right-hand value. - Less than or Equal to (
<=
): Checks if the left-hand value is less than or equal to the right-hand value.