Relational operators are used to compare two values. The result of this comparison is a Boolean value: either true
or false
. These operators help us to make decisions in our programs based on the relationships between values.
Here are the six main relational operators in Java:
==
(equal to): Checks if two values are equal.
!=
(not equal to): Checks if two values are not equal.
>
(greater than): Checks if the left value is greater than the right value.
<
(less than): Checks if the left value is less than the right value.
>=
(greater than or equal to): Checks if the left value is greater than or equal to the right value.
<=
(less than or equal to): Checks if the left value is less than or equal to the right value.
==
(equal to):
int a = 5;
int b = 5;
boolean result = (a == b); // true, because 5 is equal to 5
!=
(not equal to):
int a = 5;
int b = 3;
boolean result = (a != b); // true, because 5 is not equal to 3
>
(greater than):
int a = 5;
int b = 3;
boolean result = (a > b); // true, because 5 is greater than 3
<
(less than):
int a = 5;
int b = 8;
boolean result = (a < b); // true, because 5 is less than 8
>=
(greater than or equal to):
int a = 5;
int b = 5;
boolean result = (a >= b); // true, because 5 is equal to 5
<=
(less than or equal to):
int a = 5;
int b = 8;
boolean result = (a <= b); // true, because 5 is less than 8
Imagine you are at an amusement park, and there is a ride that requires you to be at least 48 inches tall to ride. We can use relational operators to check if a person can go on the ride:
int requiredHeight = 48; // inches
int personHeight = 50; // inches
boolean canRide = (personHeight >= requiredHeight); // true, because 50 is greater than 48
Here is a simple program that demonstrates the use of relational operators:
public class RelationalOperatorsExample {
public static void main(String[] args) {
int number1 = 10;
int number2 = 20;
// Using relational operators
boolean isEqual = (number1 == number2);
boolean isNotEqual = (number1 != number2);
boolean isGreater = (number1 > number2);
boolean isLess = (number1 < number2);
boolean isGreaterOrEqual = (number1 >= number2);
boolean isLessOrEqual = (number1 <= number2);
// Printing the results
System.out.println("number1 == number2: " + isEqual);
System.out.println("number1 != number2: " + isNotEqual);
System.out.println("number1 > number2: " + isGreater);
System.out.println("number1 < number2: " + isLess);
System.out.println("number1 >= number2: " + isGreaterOrEqual);
System.out.println("number1 <= number2: " + isLessOrEqual);
}
}
Problem: Write a Java program to compare the age of two people and determine if one person is older, younger, or the same age as the other.
Example:
int agePerson1 = 25;
int agePerson2 = 30;
// Compare the ages and print the results
Expected Output:
Person 1 is younger than Person 2.
Hint: Use relational operators to compare agePerson1
and agePerson2
and print appropriate messages.
This reading material should help you understand relational operators and how they are used in Java programming. Experiment with the examples, write your own comparisons, and try the programming problem to strengthen your understanding. Happy coding!