
6 Essential Relational Operators in Java to Boost Your Skills
Relational Operators in Java
Relational Operators in Java, also known as comparison operators, are used to compare two values. These operators return a boolean value (true
or false
) based on the comparison. They are commonly used in decision-making structures such as conditional statements (if
, else
) and loops.
List of Relational Operators in Java
Java provides six relational operators:
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Equal to (==
)
The ==
operator checks if two values are equal. If they are, it returns true
; otherwise, it returns false
.
Example:
public class ComparisonExample { public static void main(String[] args) { int a = 10; int b = 10; System.out.println(a == b); // true int x = 5; int y = 8; System.out.println(x == y); // false } }
Not Equal to (!=
)
The !=
operator checks if two values are not equal. It returns true
if they are different and false
if they are the same.
Example:
public class NotEqualExample { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a != b); // true double x = 5.5; double y = 5.5; System.out.println(x != y); // false } }
Greater Than (>
)
The >
operator checks if the left operand is greater than the right operand. It returns true
if the condition is met; otherwise, it returns false
.
Example:
public class GreaterThanExample { public static void main(String[] args) { int a = 15; int b = 10; System.out.println(a > b); // true double x = 4.5; double y = 6.7; System.out.println(x > y); // false } }
Less Than (<
)
The <
operator checks if the left operand is smaller than the right operand. It returns true
if the condition is met; otherwise, it returns false
.
Example:
public class LessThanExample { public static void main(String[] args) { int a = 8; int b = 12; System.out.println(a < b); // true double x = 9.9; double y = 3.2; System.out.println(x < y); // false } }
Greater Than or Equal to (>=
)
The >=
operator checks if the left operand is greater than or equal to the right operand. It returns true
if the condition is satisfied; otherwise, it returns false
.
Example:
public class GreaterThanOrEqualExample { public static void main(String[] args) { int a = 10; int b = 10; System.out.println(a >= b); // true double x = 15.0; double y = 10.5; System.out.println(x >= y); // true } }
Less Than or Equal to (<=
)
The <=
operator checks if the left operand is less than or equal to the right operand. It returns true
if the condition is met; otherwise, it returns false
.
Example:
public class LessThanOrEqualExample { public static void main(String[] args) { int a = 5; int b = 10; System.out.println(a <= b); // true double x = 7.5; double y = 7.5; System.out.println(x <= y); // true } }
Using Relational Operators in Conditional Statements
Relational operators are most commonly used in if
statements and loops.
Example with if
statement:
public class IfExample { public static void main(String[] args) { int age = 18; if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote."); } } }
Example with while
loop:
public class WhileExample { public static void main(String[] args) { int i = 1; while (i <= 5) { System.out.println("Count: " + i); i++; } } }
Performance Considerations
When using relational operators in Java, performance considerations arise in the following cases:
Floating-Point Comparisons
Comparing floating-point numbers (float
, double
) can be unreliable due to precision issues. Instead of ==
, use an acceptable tolerance range.
Example:
public class FloatingPointComparison { public static void main(String[] args) { double a = 0.1 + 0.2; double b = 0.3; System.out.println(a == b); // false (due to precision errors) // Better approach double epsilon = 0.00001; System.out.println(Math.abs(a - b) < epsilon); // true } }
String Comparisons
Using ==
for string comparisons can lead to unexpected results due to memory reference comparison. Always use equals()
or compareTo()
for reliable results.
Performance of compareTo()
The compareTo()
method for strings runs in O(n)
time complexity, where n
is the length of the string. If performance is critical, avoid unnecessary calls.
Object Comparison
For custom objects, ==
compares references, not content. Implement the equals()
method properly to ensure accurate comparisons.
Example:
class Person { String name; Person(String name) { this.name = name; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Person person = (Person) obj; return name.equals(person.name); } } public class ObjectComparison { public static void main(String[] args) { Person p1 = new Person("Alice"); Person p2 = new Person("Alice"); System.out.println(p1 == p2); // false System.out.println(p1.equals(p2)); // true } }
Key Takeaways
- Relational operators compare two values and return a boolean result.
- They are commonly used in control flow statements (
if
,while
,for
). ==
checks for equality,!=
checks for inequality.>
,<
,>=
, and<=
compare values numerically.- Relational operators work with both integer and floating-point numbers.
Use
.equals()
to compare string content.Be cautious when comparing floating-point numbers due to precision issues.
Understanding relational operators in Java is fundamental to writing effective Java programs, as they allow for decision-making and control flow within code. Mastering these will help you create more dynamic and interactive applications.
You can read about all operators in Java here. If you like this post, please comment and share. Visit my blog to read more Java related articles.
About the author : Mohit Jain
I have more than five years of work experience as a Java Developer. I am passionate about teaching and learning new technologies. I specialize in Java 8, Hibernate, Spring Framework, Spring Boot, and databases like MySQL, and Oracle. I like to share my knowledge with others in the form of articles.