← Back to Home

Relational Operators

Relational operators in Java are used to compare two values and return a boolean result (true or false). They are fundamental for decision-making, conditions, and control flow in Java programs.

This topic is frequently asked in interviews and heavily used in real-time applications.

What Are Relational Operators?

  • Compare two operands
  • Return a boolean result
  • Used in if, while, for, and switch conditions

List of Relational Operators in Java

Operator Name Description
> Greater than True if left > right
< Less than True if left < right
>= Greater than or equal to True if left ≥ right
<= Less than or equal to True if left ≤ right
== Equal to True if both operands are equal
!= Not equal to True if operands are not equal

1. Greater Than (>)

int a = 10;
int b = 5;
System.out.println(a > b);  // true
          

2. Less Than (<)

int x = 5;
int y = 10;
System.out.println(x < y);  // true
          

3. Greater Than or Equal To (>=)

int score = 60;
System.out.println(score >= 60); // true
          

4. Less Than or Equal To (<=)

int age = 18;
System.out.println(age <= 18); // true
          

5. Equal To (==)

With Primitive Data Types

int a = 10;
int b = 10;
System.out.println(a == b); // true
          

With Reference Data Types (Important!)

String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);      // false
System.out.println(s1.equals(s2)); // true
          

Why it matters: == compares references, not content, for objects.

6. Not Equal To (!=)

int x = 10;
int y = 20;
System.out.println(x != y); // true
          

Type Compatibility Rules

  • Relational operators work with:
    • Numeric types
    • char
  • boolean cannot be compared using relational operators
boolean a = true;
boolean b = false;
// a > b ❌ invalid
          

Relational Operators with char

char c1 = 'A';  // 65
char c2 = 'B';  // 66
System.out.println(c1 < c2); // true
          

Relational Operators with Floating-Point Numbers

double x = 0.1 + 0.2;
System.out.println(x == 0.3); // false (precision issue)
          

Why it matters: Floating-point comparison can be unreliable.

Best Practice for Object Comparison

  • Use == → for primitives
  • Use .equals() → for objects
Integer a = 100;
Integer b = 100;
System.out.println(a.equals(b)); // true
          

Common Beginner Mistakes

  • Using == instead of .equals() for objects
  • Comparing floating-point values directly
  • Trying relational operators with boolean
  • Confusing = and ==

Interview-Ready Answers

Short Answer

Relational operators compare two values and return a boolean result.

Detailed Answer

Java relational operators (>, <, >=, <=, ==, !=) are used to compare operands and return true or false. They are commonly used in conditional statements and loops. For object comparison, .equals() should be used instead of ==.

Key Takeaway

Relational operators drive conditional logic in Java. Correct usage—especially understanding the difference between == and .equals()—is essential for bug-free programs.