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. Therefore, relational operators are also called comparison operators.

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

We can perform these comparisons with the help of relational operators in Java. Relational operators require two operands.

Java supports six types of relational operators. The list of relational operators is in the below table.

                                  Java Six Relational Operators

OperatorsMeaningDescription
1. <Less thanThis operator checks whether the value of the left operand is less than the value of the right operand. If the value on the left side of operator is less than the value on the right side, the result becomes true.
2. <=Less than or equal toThis operator evaluates that the value of the left operand is less than or equal to the value of the right operand. If it is the case, the operator returns true.
3. >Greater thanThis operator evaluates that the value of the left operand is greater than the value of the right operand. If the value on left side of operator is greater than the value on right side, the greater than (>) operator returns true.
4. >=Greater than or equal toThis operator tests that the value of the left operand is greater than or equal to the value of the right operand. If the value on the left side of the operator is greater than or equal to the value on the right side, then the operator returns true.
5. ==Equal toThis operator evaluates that the value of left and right operands are equal or not. If values on both sides of the operator are equal to each other, the equal operator (==) returns true.
6. !=Not equal toThis operator evaluates that the left operand is not equal to the right side. If the values on both sides of operator are not equal to each other, the not equal operator returns true.

The above six comparison operators provide the foundation for comparing data in Java. Each operator is always a combination of two constants, variables, expressions, or a mix of these.


Key points:

1. The result of all relational operators is always of a boolean type. It returns always true or false.


2. Relational operators are mainly used to create conditions in decision-making statements, like this:

if(condition_is_true) 
    statement to be executed.
// We can implement this statement in the program like this:
   if(x > y) 
      System.out.println(x);

Java Comparison Operators Example Programs


Let’s create a simple program based on the all six types of java relational operators.

Program 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 < y)); 

 System.out.println("y is greater than equal x: " +(y >= 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)); 
 } 
}
Output: 
       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 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"); 
 } 
else 
{ 
  System.out.println("y is less than x"); 
 } 
if(y < z)
{ 
  System.out.println("y is less than z"); 
 } 
else 
{ 
  System.out.println("y is greater than z"); 
 } 
} 
public static void main(String[] args) 
{ 
// Create an object of class. 
   Test t = new Test(); 
   t.compare(); // Calling compare method using reference variable t. 
 } 
}
Output: 
       y is greater than x 
       y is less than z

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


Key Points:

1. Relational operators in Java are the most frequently used operators in the expressions that control the if statement and different loop statements.

2. These operators are mainly used to determine equality and order.

3. The six relational operators are < , > , <= , >= , == , and != .

4. Equality in Java is represented by two equal signs, not one. Note that a single equal sign is an assignment operator.

5. In Java, integers, floating-point numbers, characters, and booleans can be compared using the equality test, ==, and inequality test, !=.


In this tutorial, you learned six relational operators in Java with example programs. Hope that you will have understood how to use relational or comparison operators in java program.
Thanks for reading!!!

Next ⇒ Logical Operators in Java⇐ PrevNext ⇒