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 Six 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:

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 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 java 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 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.

Hope that this tutorial has covered important basic points related to java relational operators with example programs. Remember the following key points.

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 with 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, !=.

If you like this tutorial, please share it on social networking sites with your friends.
Thanks for reading!!!

Next ⇒ Logical Operators in Java⇐ PrevNext ⇒

Leave a Comment