๐ 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:
ifstatements- 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
| Operator | Meaning |
|---|---|
> | 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
| Operator | Returns |
|---|---|
> | 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.

