Relational Operators in Java

Relational Operators in Java

In Java, relational operators help you compare values, but using them correctly is important. Whether you are comparing numbers, characters, or object references, knowing how these operators work can save you from errors and confusion.

In this guide, we’ll explain relational operators in Java, show you how they are used, and provide clear examples. By the end, you will understand how to compare values in Java with ease and avoid common mistakes.

Table of Contents:

What are Relational Operators in Java?

The relational operators in Java are used to check the relationship between the two operands. The relationship here is the comparison of operands for equality, non-equality, less than, or greater than. 

The Relational operators are also commonly called comparison operators, and they return a value of true or false. 

Types of Relational Operators in Java

Relational operators in Java have 6 different types. These are

Here is the list of Relational operators in Java.

Types of Relational Operator in Java

Let x and y be two integer values.

Operator Name Description Syntax
== Equal Returns true if both operands are equal. For numerics, it compares values. For objects, it compares memory locations. x==y
!= Not Equal Returns true if the operands are not equal. For numerics, it compares values. For objects, it compares memory locations. x!=y
< Less than Returns true if the value of x is less than the value of y x<y
> Greater than Returns true if the value of x is greater than the value of y; otherwise, returns false. x>y
<= Less than or equal to Returns true if the value of x is less than or equal to the value of y; otherwise, returns false. x<=y
>= Greater than or equal to Returns true if the value of x is greater than or equal to the value of y; otherwise, returns false. x>=y

Note: x and y are two integer values.

Now, let us discuss each one of them in detail.

1. Equal Operator (==)

The Equal Operator returns true if both operands are equal. 

For primitive data types, it compares the values of the variable.

For objects, it compares references, i.e., whether they point to the same memory location or not.

Example: 

Java

Output:

Equal Operator
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

2. Not Equal Operator (!=)

The Not Equal Operator (!=) returns false if both operands have the same values; otherwise, it returns true.

Example: 

Java

Output:

Not Equal Operator

3. Less than Operator (<)

The Less than Operator (<) returns true if the value of operand1 is less than the value of operand2.

Example: 

Java

Output:

Less than Operator

4. Greater than Operator (>)

The Greater Than Operator (>) returns true if the value of operand1 is greater than the value of operand2.

Example: 

Java

Output:

Greater than Operator

Note: Relational operators like <, >, <=, >= are not applicable to boolean types, only == and != are allowed

5. Less Than or Equal to Operator (<=)

The Less Than or Equal to Operator (<=) returns true if the value of operand1 is less than or equal to the value of operand2; otherwise, it returns false.

Example: 

Java

Output:

Less Than or Equal to Operator

6. Greater Than or Equal to Operator (>=)

The Greater Than or Equal to Operator (>=) returns true if the value of operand1 is greater than or equal to the value of operand2; otherwise, it returns false.

Example: 

Java

Output:

Greater Than or Equal to Operator

Operator Behavior with Different Types of Relational Operators in Java

The relational operators work differently with different data types. Let us discuss them in detail.

1. Numeric Comparison

In this comparison, the relational operators compare the actual numeric values of the variables.

Example: 

Java

Output:

 Numeric Comparison

2. Reference Type Comparison

In this comparison, the relational operators compare the memory location of the two objects.

The == operator is used to compare the memory location, and the .equals() method is used to compare content equality for objects like Strings.

Example: 

Java

Output:

Reference Type Comparison

Get 100% Hike!

Master Most in Demand Skills Now!

3. Boolean Comparison

In this type of comparison, the relational operators compare the Boolean values of the variables.

Only == and != work for booleans. Operators like <, >, <=, >= do not apply to booleans and will cause compilation errors.

Example: 

Java

Output:

Boolean Comparison

4. Character Comparison 

Relational operators are not limited only to comparing numerical or boolean values. You can use them to compare the ASCII value of each character.

Example: 

Java

Output:

Character Comparison

Real-World Usage of Relational Operators

1. Conditional Statements 

The Relational operators in Java are most widely used in conditional statements to control the flow of a program as per the conditions like if, else if, and else.

Example of Finding the Greatest Among Three Numbers: 

Java

Output:

Conditional Statements 

2. Loops

The Relational operators in Java are also used in different loops like the for loop, while loop, or do-while loop. They mainly define the conditions in these loops to avoid an infinite loop or unpredictable results.

Example of a For Loop:

Java

Output:

Loops

Example of a While Loop:

Java

Output:

Example of a While Loop

Example of a Do-While Loop:

Java

Output:

Example of a Do-While Loop

3. Sorting 

Sorting is the process of arranging the elements in ascending or descending order as per the requirements. 

The relational operators in Java play a crucial role in the process of sorting. They are used to compare different elements and arrange them as required.

Bubble Sort Example in Java:

Java

Output:

 Sorting 

Best Practices for Using Relational Operators in Java

1. Use the parentheses () for the complex comparisons 

2. Be careful when using the floating-point numbers, due to the rounding of the numbers.

3. Use the .equals() method when comparing the objects.

4. Always compare compatible data types to avoid unexpected results.

Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

Conclusion

Relational operators in Java are used to compare the values of two variables. These operators are commonly used in conditional statements, loops, and sorting. The main types of relational operators are == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). It’s important to use these operators carefully to avoid unexpected results in your program.

If you want to learn more about Java, you can refer to our Java Course.

Relational Operators in Java – FAQs

Q1. What is the output of relational operators?

The output given by the relational operator is in the form of a Boolean value, i.e., True or False.

Q2. What does != mean in coding?

The not-equal-to operator ( != ) returns true if the operands don’t have the same value; otherwise, false.

Q3. What is << in Java?

This is the signed left shift operator ” << ", which shifts a bit pattern to the left.

Q4. Which is the relational operator?

A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities

Q5. What are the types of operators in Java?

Java supports several types of operators, like arithmetic, relational, logical, bitwise, assignment, unary, ternary, and shift operators.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner