Java Relational Operators Explained: ==, !=, >, <, >=, <= with Examples

0

Programs constantly need to make decisions by comparing values. Is one number greater than another? Are two values equal? Has a balance reached zero? Is a student eligible for a particular result? Java uses relational operators to answer these questions.

A relational operator compares two values and produces a boolean result: either true or false. This makes relational operators a foundation for decision-making, loops, validation, searching, filtering, and many other programming tasks.

Why Relational Operators Matter

Think of a relational operator as a question asked to Java. For example, age >= 18 asks, "Is age at least 18?" Java evaluates that question and produces either true or false.

Important: Relational operators do not change the values being compared. They examine values and produce a boolean result.

Relational Operators at a Glance

Operator Name Meaning Example
== Equal to Checks whether two values are equal 10 == 10 → true
!= Not equal to Checks whether two values are different 10 != 5 → true
> Greater than Checks whether the left value is greater 10 > 5 → true
< Less than Checks whether the left value is smaller 5 < 10 → true
>= Greater than or equal to Checks whether the left value is greater or equal 10 >= 10 → true
<= Less than or equal to Checks whether the left value is smaller or equal 5 <= 10 → true

Basic Syntax

value1 operator value2

The expression compares value1 with value2 and produces a boolean result.

int age = 20;

boolean result = age >= 18;

System.out.println(result);

Since 20 is greater than or equal to 18, the result is true.

Equal To Operator (==)

The == operator checks whether two operands have equal values.

int first = 100;
int second = 100;

boolean result = first == second;

System.out.println(result);

Both variables contain 100, so the expression produces true.

For primitive values, == compares the actual values. With reference types such as objects, however, == checks whether two references refer to the same object. That distinction becomes important when working with String and other objects.

Common trap: Do not confuse = with ==. The first performs assignment; the second performs comparison.

Not Equal Operator (!=)

The != operator checks whether two values are different.

int passwordAttempts = 3;

boolean result = passwordAttempts != 0;

System.out.println(result);

Because 3 is not equal to 0, the result is true.

This operator is useful when a program needs to continue processing while a value has not reached a particular state.

Greater Than Operator (>)

The > operator checks whether the left-hand value is strictly greater than the right-hand value.

int salary = 60000;

boolean result = salary > 50000;

System.out.println(result);

Since 60000 is greater than 50000, Java produces true.

Notice that if both values were exactly equal, the result would be false. The greater-than operator does not include equality.

Less Than Operator (<)

The < operator checks whether the left-hand value is strictly smaller than the right-hand value.

int temperature = 15;

boolean result = temperature < 20;

System.out.println(result);

Because 15 is less than 20, the result is true.

Greater Than or Equal To (>=)

The >= operator checks two possibilities: the left value can either be greater than the right value or exactly equal to it.

int age = 18;

boolean eligible = age >= 18;

System.out.println(eligible);

The result is true because the value is exactly 18. This operator is particularly useful for minimum requirements and boundaries.

Less Than or Equal To (<=)

The <= operator checks whether the left value is smaller than or equal to the right value.

int attempts = 3;

boolean allowed = attempts <= 3;

System.out.println(allowed);

The result is true because the value is exactly at the permitted maximum.

Relational Operators with if Statements

Relational operators become especially powerful when combined with decision-making statements such as if.

int marks = 75;

if (marks >= 40) {
    System.out.println("Pass");
}

The expression marks >= 40 produces true, so Java executes the statement inside the if block.

This pattern appears everywhere in application development: checking permissions, validating input, enforcing limits, comparing prices, testing thresholds, and deciding what the application should do next.

Relational Operators with Variables

Comparisons become more useful when values come from variables rather than fixed numbers.

int availableStock = 15;
int requestedStock = 20;

boolean enoughStock = availableStock >= requestedStock;

System.out.println(enoughStock);

The expression evaluates to false because fifteen items are not enough to satisfy a request for twenty.

This is a small example of how relational operators turn business rules into executable logic.

Comparing Floating-Point Values

Comparing float and double values requires additional care because floating-point numbers cannot represent every decimal value exactly in binary.

double value = 0.1 + 0.2;

System.out.println(value == 0.3);

You should not build critical floating-point comparisons around the assumption that mathematically equivalent decimal calculations will always produce exactly identical binary representations.

For calculations where exact decimal behavior matters, such as financial applications, a decimal-oriented approach such as BigDecimal is often more appropriate.

Comparing Strings: An Important Distinction

One of the most common Java mistakes is using == when the intention is to compare the contents of two strings.

String first = new String("Java");
String second = new String("Java");

System.out.println(first == second);

This does not compare the text content. It compares object references, so the result can be false even though both objects contain the same characters.

To compare string content, use equals():

String first = new String("Java");
String second = new String("Java");

System.out.println(first.equals(second));

Now Java compares the contents of the strings, producing true.

Remember: For primitive values, == compares values. For object references, == compares references. For string content, use equals().

Common Beginner Mistakes

  • Using = when a comparison with == is required.
  • Forgetting that > and < do not include equality.
  • Using == to compare the contents of strings.
  • Assuming relational operators return numeric values. Their result is a boolean.
  • Comparing floating-point values using exact equality without considering floating-point precision.
  • Writing complicated comparison expressions without parentheses when the intended logic is not immediately obvious.

Practical Example: Eligibility Check

Consider a simple application that checks whether a customer is eligible for a special offer based on age and purchase amount.

int age = 25;
double purchaseAmount = 5000.0;

boolean ageEligible = age >= 21;
boolean amountEligible = purchaseAmount >= 3000.0;

System.out.println(ageEligible);
System.out.println(amountEligible);

The first comparison checks the minimum age requirement, while the second checks the minimum purchase amount. Each comparison produces an independent boolean result that can later be combined using logical operators.

Best Practices

  • Choose the operator that exactly represents the business rule.
  • Use meaningful boolean variable names such as isEligible, hasStock, or isValid.
  • Use parentheses when they make complex conditions easier to understand.
  • Use equals() when comparing string contents.
  • Be cautious when comparing floating-point values for exact equality.
  • Keep comparisons simple enough that another developer can understand the business rule quickly.

Interview Insights

A classic interview question is the difference between = and ==. The answer is fundamental: = assigns a value, whereas == compares two operands.

Another frequently tested concept is string comparison. A candidate should know that == compares object references when used with reference types, while equals() can be used to compare string contents.

Quick Revision

Operator Question It Asks Example Result
== Are both values equal? 10 == 10 true
!= Are the values different? 10 != 5 true
> Is the left value greater? 10 > 5 true
< Is the left value smaller? 5 < 10 true
>= Is the left value greater or equal? 10 >= 10 true
<= Is the left value smaller or equal? 5 <= 10 true

Final Takeaway

Relational operators transform comparisons into boolean facts that Java can use for decision-making. Once you are comfortable with ==, !=, >, <, >=, and <=, you have one of the essential building blocks of Java logic. The real skill is not memorizing the symbols—it is choosing the correct comparison and understanding exactly what Java is comparing.

Post a Comment

0Comments
Post a Comment (0)