Relational Operators in Java | Example Program

Relational operators in Java are those operators that are used to perform the comparison between two numeric values or two quantities. These operators determine the relationship between them by comparing operands.

For example, to know which is a bigger number, comparing the age of two persons, comparing the price of two items, etc.

These comparisons can be performed with the help of relational operators. Relational operators require two operands.

Java supports six types of relational operators. They are listed in the below table.

                                  Java’s Relational Operators

              Operators                              Meaning
                   1. < Less than
                   2. <= Less than or equal to
                   3. > Greater than
                   4. >= Greater than or equal to
                   5. == Equal to
                   6. != Not equal to

Key points:
👉 The result of all relational operators is always of a boolean type. It returns always true or false.
👉 Relational operators are mainly used to create conditions in decision statements, like this:

if(condition_is_true) statement to be executed.
This statement can be implemented in the program like this:
 if(x > y) 

Relational Operators Example Programs

Let’s create a simple program based on the relational operators concept.
Program source code 1:

package relationalPrograms; 
public class RelationalOperatorsDemo 
public static void main(String[] args) 
 int x = 10; 
 int y = 30; 
System.out.println("x = " +x+ "y = " +y); 
System.out.println("x is greater than y: " +(x>y)); 
System.out.println("x is less than y: " +(x=x)); 
System.out.println("x is less than equal to y: " +(x<=y)); 
System.out.println("x is equal to y: " +(x==y)); 
System.out.println("(x+20 < y+10): " +(x+20 < y+10)); 
       x = 10 y = 30 
       x is greater than y: false 
       x is less than y: true 
       y is greater than equal to x: true 
       x is less than equal to y: true 
       x is equal to y: false 
       (x+20 < y+10): true

In the preceding example program, the value of the relational expressions is either true or false. If the condition is true, it returns true otherwise return false statement.

The arithmetic expression (x + 20) and (y + 10) are evaluated first and then the result is compared between them because arithmetic operators have a higher priority over relational operators.

Let’s create another program where we will use the if-else statement to compare among three numeric values.
Program source code 2:

package relationalPrograms; 
public class Test 
 int x = 30; 
 float y = 50.5F; 
 int z = 60; 
void compare()
 if(y > x)
   System.out.println("y is greater than x"); 
  System.out.println("y is less than x"); 
if(y < z)
  System.out.println("y is less than z"); 
  System.out.println("y is greater than z"); 
public static void main(String[] args) 
// Create an object of class. 
   Test t = new Test();; // Calling compare method using reference variable t. 
       y is greater than x 
       y is less than z

In the above example program, if you have difficulty to understand the concept of if-else statement, you can skip it. When you will learn this topic in the decision-making chapter, you can easily understand this program.

Final words
Hope that this tutorial has covered almost all important points related to relational operators in java with basic example programs. I hope that you will have understood java relational operators and enjoyed them.

If you like this tutorial, please share it on social networking sites for your friends.
Thanks for reading!!!
Next ⇒ Logical Operators in Java⇐ PrevNext ⇒

Leave a Comment