Relational Operators in Java Explained with Examples and Common Mistakes

2-Relational Operators can be really tricky in Java!

๐Ÿ” Relational Operators in Java

๐Ÿ“Œ Introduction

Relational operators are used to compare two values and determine the relationship between them. Unlike arithmetic operators, which return numeric results, relational operators always return a boolean value โ€” either true or false.

These operators are the foundation of decision-making in Java and are heavily used in:

  • if statements
  • loops (for, while)
  • conditional expressions

Understanding relational operators clearly is essential before moving on to control statements.


๐Ÿง  What Are Relational Operators?

๐Ÿ“˜ Definition

Relational operators compare two operands and check whether a specific relationship exists between them.

โœ” Result is always:

true  or  false

๐Ÿ“Š List of Relational Operators in Java

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Equal to
!=Not equal to

๐Ÿ”ผ 1. Greater Than Operator (>)

๐Ÿ“Œ Basic Usage

Checks whether the left operand is greater than the right operand.

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

โœ” Output:

true

โš ๏ธ Common Confusion
System.out.println(5 > 10);

โœ” Output:

false

๐Ÿง  Relational operators do not swap operands automatically.


โš ๏ธ Tricky Case with Characters
System.out.println('b' > 'a');

โœ” Output:

true

๐Ÿง  Reason:
Characters are compared using their Unicode values.


๐Ÿ”ฝ 2. Less Than Operator (<)

๐Ÿ“Œ Basic Usage
int x = 3;
int y = 7;
System.out.println(x < y);

โœ” Output:

true

โš ๏ธ Tricky Expression
System.out.println(10 < 5 + 10);

โœ” Output:

true

๐Ÿง  Arithmetic expressions are evaluated before relational operators.


๐Ÿ”ผโž– 3. Greater Than or Equal To (>=)

๐Ÿ“Œ Basic Usage
int marks = 40;
System.out.println(marks >= 40);

โœ” Output:

true

โš ๏ธ Common Mistake
System.out.println(10 => 5); // INVALID

โŒ Compilation Error
๐Ÿง  Operator must be written as >=, not =>.


๐Ÿ”ฝโž– 4. Less Than or Equal To (<=)

๐Ÿ“Œ Basic Usage
int age = 18;
System.out.println(age <= 18);

โœ” Output:

true

โš ๏ธ Confusion with Assignment
if (x = 10)   // ERROR

โŒ Compilation Error
๐Ÿง  Assignment (=) cannot be used inside conditions.


โš–๏ธ 5. Equal To Operator (==)

๐Ÿ“Œ Basic Usage

Checks whether two values are equal.

int a = 5;
int b = 5;
System.out.println(a == b);

โœ” Output:

true

โš ๏ธ MOST COMMON CONFUSION (VERY IMPORTANT)
int a = 10;
if (a = 5) {
    System.out.println("Hello");
}

โŒ Compilation Error
๐Ÿง  = is assignment, not comparison.

โœ” Correct:

if (a == 5) {
    System.out.println("Hello");
}

โš ๏ธ Tricky Case with Floating Point
double x = 0.1 + 0.2;
System.out.println(x == 0.3);

โœ” Output:

false

๐Ÿง  Reason:
Floating-point numbers have precision issues.

โœ” Correct approach:

System.out.println(Math.abs(x - 0.3) < 0.0001);

โŒ 6. Not Equal To Operator (!=)

๐Ÿ“Œ Basic Usage
int a = 5;
int b = 7;
System.out.println(a != b);

โœ” Output:

true

โš ๏ธ Tricky Example
System.out.println(10 != 10);

โœ” Output:

false

โš ๏ธ Relational Operators with Non-Primitive Types (IMPORTANT)

โŒ Wrong Way (Strings)
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);

โœ” Output:

true

โš ๏ธ This works accidentally due to string pool.


โŒ Dangerous Case
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);

โœ” Output:

false

๐Ÿง  == compares references, not content.

โœ” Correct way:

System.out.println(s1.equals(s2));

โš ๏ธ Common Compilation Errors & Mistakes

โŒ 1. Using = instead of ==
โŒ 2. Using relational operators with boolean incorrectly
System.out.println(true > false); // ERROR
โŒ 3. Comparing incompatible types
System.out.println(10 > "Java"); // ERROR
โŒ 4. Expecting relational operators to return numbers
int x = (10 > 5); // ERROR

Relational operators return boolean, not integers.


๐Ÿง  Operator Precedence Reminder

๐Ÿ“Œ Important Rule

Arithmetic operators are evaluated before relational operators.

System.out.println(10 + 5 > 12);

โœ” Output:

true

๐Ÿ“Š Summary Table

OperatorReturns
>boolean
<boolean
>=boolean
<=boolean
==boolean
!=boolean

๐ŸŽฏ Why Relational Operators Are Important

โญ Key Importance
  • Core of decision making
  • Used in conditions and loops
  • Foundation for logical operators
  • Frequently tested in exams and interviews

๐Ÿ Conclusion

๐Ÿ“ Final Summary

Relational operators allow Java programs to make decisions by comparing values. While they appear simple, incorrect usage โ€” especially confusing = with ==, comparing floating-point values, or using them with objects โ€” can lead to logical or compilation errors.

A strong understanding of relational operators is essential before learning logical operators and control statements, which depend heavily on boolean expressions.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *