Relational operators are used to compare two values in Java. They help determine whether one value is greater than, less than, or equal to another value.
These operators are commonly used in if statements, loops, conditional expressions, and decision-making logic.
Although they look simple, relational operators have a few important rules that every Java developer should know—especially for interviews.
Let's explore them with simple explanations and practical examples.
What Are Relational Operators?
Java provides four relational operators.
| Operator | Description |
|---|---|
< |
Less than |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
Each relational operator compares two operands and always returns a boolean value (true or false).
Example
System.out.println(10 < 20);
System.out.println(20 >= 20);
System.out.println(50 > 100);
Output
true
true
false
Rule 1: Relational Operators Work on All Primitive Types Except boolean
Relational operators can be applied to every primitive data type except boolean.
Supported primitive types include:
byteshortintlongfloatdoublechar
Valid Examples
System.out.println(10 < 10.5);
Output
true
System.out.println('a' > 100.5);
Output
false
Explanation
'a' = 97
97 > 100.5
↓
false
System.out.println('b' > 'a');
Output
true
Explanation
'b' = 98
'a' = 97
98 > 97
↓
true
System.out.println('A' < 'a');
Output
true
Explanation
'A' = 65
'a' = 97
65 < 97
↓
true
Invalid Example
System.out.println(true > false);
Compile-time error
bad operand types for binary operator '>'
Why?
The values true and false are logical values, not numeric values.
Since there is no mathematical concept of greater than or less than between boolean values, Java does not allow relational operators on boolean.
Rule 2: Relational Operators Cannot Be Applied to Objects
Relational operators work only on primitive numeric values.
They cannot be used with objects such as:
StringIntegerEmployee- Any user-defined class
Example
System.out.println("rajesh123" > "rajesh");
Compile-time error
bad operand types for binary operator '>'
Why?
Although "rajesh123" is longer than "rajesh", Java does not compare objects using relational operators.
For strings, use the compareTo() method instead.
Example
System.out.println("apple".compareTo("banana"));
Output
-1
Similarly,
System.out.println("banana".compareTo("apple"));
Output
1
And,
System.out.println("java".compareTo("java"));
Output
0
Rule 3: Nesting Relational Operators Is Not Allowed
Many beginners try to write mathematical expressions like this:
System.out.println(10 > 20 > 30);
This results in a compile-time error.
Why?
Java evaluates relational operators from left to right.
So the expression becomes
(10 > 20) > 30
The first comparison is
10 > 20
↓
false
Now Java tries to evaluate
false > 30
which is equivalent to
boolean > int
This is invalid.
Compile-time error
bad operand types for binary operator '>'
Correct Way to Write Multiple Comparisons
Instead of
10 < 20 < 30
write
(10 < 20) && (20 < 30)
Output
true
This is the correct way to express mathematical chain comparisons in Java.
Character Comparisons
Since the char data type stores Unicode values internally, characters can be compared using relational operators.
Example
System.out.println('b' > 'a');
Output
true
because
98 > 97
System.out.println('A' < 'a');
Output
true
because
65 < 97
System.out.println('z' > 'm');
Output
true
because
122 > 109
Common Beginner Mistakes
Comparing Strings Using >
Incorrect
String s1 = "apple";
String s2 = "banana";
System.out.println(s1 > s2);
Compile-time error
Always use
s1.compareTo(s2)
or
s1.equals(s2)
depending on your requirement.
Using Relational Operators with boolean
Incorrect
true > false
Compile-time error
Boolean values cannot be compared using relational operators.
Writing Mathematical Chains
Incorrect
10 < 20 < 30
Correct
(10 < 20) && (20 < 30)
Summary Table
| Scenario | Allowed? | Example |
|---|---|---|
| Numeric types | ✅ Yes | 10 < 20.5 |
| Character comparison | ✅ Yes | 'b' > 'a' |
boolean values |
❌ No | true > false |
Objects (String, etc.) |
❌ No | "abc" > "ab" |
| Nested comparisons | ❌ No | 10 < 20 < 30 |
Best Practices
- Use relational operators only with primitive numeric types.
- Never compare strings using
<,<=,>, or>=. - Use
compareTo()for ordering strings. - Use
equals()when checking whether two strings have the same content. - Use logical operators (
&&,||) to combine multiple relational expressions.
Quick Memory Trick 🧠
Relational operators work only on numeric primitive values.
Remember:
- ✅ Numbers → Allowed
- ✅ Characters → Allowed
- ❌ Booleans → Not Allowed
- ❌ Objects → Not Allowed
- ❌ Chain comparisons → Not Allowed
Key Takeaways
- Java provides four relational operators:
<,<=,>, and>=. - Every relational operator returns a
booleanvalue. - They work with all primitive numeric data types, including
char. - They cannot be applied to
boolean. - They cannot be applied to object types like
String. - Nested comparisons such as
10 < 20 < 30are not valid Java syntax. - Use
compareTo()for ordering strings andequals()for equality checks. - Combine relational expressions using logical operators when multiple conditions are required.
Happy Coding!
Top comments (0)