In this lesson, you will learn.
Relational operators in Java are used to compare two values and determine the relationship between them.
These operators return a boolean result, indicating whether the specified relationship holds true or false.
The common relational operators in Java include.
==):Checks if two values are equal.
int a = 5;
int b = 7;
boolean isEqual = (a == b); // false
!=):Check if the two values are not equal.
int x = 10;
int y = 10;
boolean isNotEqual = (x != y); // false
>):Checks if the left operand is greater than the right operand.
double p = 3.14;
double q = 2.71;
boolean isGreater = (p > q); // true
<):Checks if the left operand is less than the right operand.
int m = 15;
int n = 20;
boolean isLess = (m < n); // true
>=)Checks if the left operand is greater than or equal to the right operand.
int num1 = 8;
int num2 = 8;
boolean isGreaterOrEqual = (num1 >= num2); // true
<=):Check if the two values are not equal.
int alpha = 5;
int beta = 7;
boolean isLessOrEqual = (alpha <= beta); // true
| Operator | Description | Example | Result e.g. x=7, y=3 |
|---|---|---|---|
== |
Equal to | x == y |
false |
!= |
Not equal to | x != y |
false |
> |
Greater than | x > y |
true |
< |
Less than | x < y |
false |
>= |
Greater than or equal to | x >= y |
true |
<= |
Less than or equal to | x <= y |
false |
Relational operators are frequently used in control flow statements, such as if statements and loops, to make decisions based on the relationships between values.
They play a crucial role in programming logic and comparisons.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.