DEV Community

Cover image for Relational Operators in Java
Rajesh Bhola
Rajesh Bhola

Posted on

Relational Operators in Java

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);
Enter fullscreen mode Exit fullscreen mode

Output

true
true
false
Enter fullscreen mode Exit fullscreen mode

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:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char

Valid Examples

System.out.println(10 < 10.5);
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

System.out.println('a' > 100.5);
Enter fullscreen mode Exit fullscreen mode

Output

false
Enter fullscreen mode Exit fullscreen mode

Explanation

'a' = 97

97 > 100.5

↓

false
Enter fullscreen mode Exit fullscreen mode

System.out.println('b' > 'a');
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Explanation

'b' = 98
'a' = 97

98 > 97

↓

true
Enter fullscreen mode Exit fullscreen mode

System.out.println('A' < 'a');
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Explanation

'A' = 65
'a' = 97

65 < 97

↓

true
Enter fullscreen mode Exit fullscreen mode

Invalid Example

System.out.println(true > false);
Enter fullscreen mode Exit fullscreen mode

Compile-time error

bad operand types for binary operator '>'
Enter fullscreen mode Exit fullscreen mode

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:

  • String
  • Integer
  • Employee
  • Any user-defined class

Example

System.out.println("rajesh123" > "rajesh");
Enter fullscreen mode Exit fullscreen mode

Compile-time error

bad operand types for binary operator '>'
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

Output

-1
Enter fullscreen mode Exit fullscreen mode

Similarly,

System.out.println("banana".compareTo("apple"));
Enter fullscreen mode Exit fullscreen mode

Output

 1
Enter fullscreen mode Exit fullscreen mode

And,

System.out.println("java".compareTo("java"));
Enter fullscreen mode Exit fullscreen mode

Output

0
Enter fullscreen mode Exit fullscreen mode

Rule 3: Nesting Relational Operators Is Not Allowed

Many beginners try to write mathematical expressions like this:

System.out.println(10 > 20 > 30);
Enter fullscreen mode Exit fullscreen mode

This results in a compile-time error.

Why?

Java evaluates relational operators from left to right.

So the expression becomes

(10 > 20) > 30
Enter fullscreen mode Exit fullscreen mode

The first comparison is

10 > 20

↓

false
Enter fullscreen mode Exit fullscreen mode

Now Java tries to evaluate

false > 30
Enter fullscreen mode Exit fullscreen mode

which is equivalent to

boolean > int
Enter fullscreen mode Exit fullscreen mode

This is invalid.

Compile-time error

bad operand types for binary operator '>'
Enter fullscreen mode Exit fullscreen mode

Correct Way to Write Multiple Comparisons

Instead of

10 < 20 < 30
Enter fullscreen mode Exit fullscreen mode

write

(10 < 20) && (20 < 30)
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

because

98 > 97
Enter fullscreen mode Exit fullscreen mode

System.out.println('A' < 'a');
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

because

65 < 97
Enter fullscreen mode Exit fullscreen mode

System.out.println('z' > 'm');
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

because

122 > 109
Enter fullscreen mode Exit fullscreen mode

Common Beginner Mistakes

Comparing Strings Using >

Incorrect

String s1 = "apple";
String s2 = "banana";

System.out.println(s1 > s2);
Enter fullscreen mode Exit fullscreen mode

Compile-time error

Always use

s1.compareTo(s2)
Enter fullscreen mode Exit fullscreen mode

or

s1.equals(s2)
Enter fullscreen mode Exit fullscreen mode

depending on your requirement.


Using Relational Operators with boolean

Incorrect

true > false
Enter fullscreen mode Exit fullscreen mode

Compile-time error

Boolean values cannot be compared using relational operators.


Writing Mathematical Chains

Incorrect

10 < 20 < 30
Enter fullscreen mode Exit fullscreen mode

Correct

(10 < 20) && (20 < 30)
Enter fullscreen mode Exit fullscreen mode

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 boolean value.
  • 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 < 30 are not valid Java syntax.
  • Use compareTo() for ordering strings and equals() for equality checks.
  • Combine relational expressions using logical operators when multiple conditions are required.

Happy Coding!

Top comments (0)