Relational Operators in Java - Types of Relational Operators
Relational operators in Java, also called comparison operators, are used to compare two values or expressions. Every relational operator returns a boolean result — either true or false — which makes them essential for conditions, loops, and decision-making.
Types of Relational Operators
1. Equal To (==)
Checks whether two values are equal.
int a = 5, b = 5;
System.out.println(a == b); // true
2. Not Equal To (!=)
Checks whether two values are not equal.
System.out.println(a != b); // false
3. Greater Than (>)
Checks whether the left value is greater than the right value.
4. Less Than (<)
Checks whether the left value is less than the right value.
5. Greater Than or Equal To (>=)
Checks whether the left value is greater than or equal to the right value.
6. Less Than or Equal To (<=)
Checks whether the left value is less than or equal to the right value.
int score = 82;
if (score >= 40) {
System.out.println("Pass");
}
Using Relational Operators with Objects
For primitive types, == compares actual values, but for objects (like String), == compares references, not content — use .equals() to compare the actual content of two objects.
==— equal to!=— not equal to>— greater than<— less than>=— greater than or equal to<=— less than or equal to
== to compare two String objects instead of .equals(), which can lead to unexpected results.Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.