Relational Operators in Java with Example

Relational operators in Java are those binary operators that are used to perform the comparison between two values or expressions.

For example, we can use relational operators to determine which number is larger, compare the ages of two people, compare the prices of two items, or check whether two values are equal.

Relational operators always require two operands to perform a comparison. They determine the relationship between operands and return a boolean result (true or false). Since they compare values or expressions, relational operators are also called comparison operators in Java.

Relational operators help us to compare values and make decisions in programs. Therefore, they are commonly used in conditions and decision-making statements such as if, if-else, and loops like while and for.

Syntax of Relational Operators in Java


The general syntax for using relational operators in Java is:

operand1 relational_operator operand2

In the above syntax:

  • operand1 → Represents the first value or expression that you want to compare.
  • relational_operator → Specifies the comparison operator, such as >, <, ==, !=, >=, or <=.
  • operand2 → Represents the second value or expression that is compared with the first operand.

The relational operator evaluates the relationship between the two operands and produces a Boolean result, either true or false.

Simple Example of Relation Operator

public class RelationalOperatorExample {
  public static void main(String[] args) {
    int a = 15;
    int b = 10;
    boolean result = a > b;
    System.out.println(result);
  }
}

Output:

true

In this example:

  • The a > b is an expression in which “a” is operand1, “>” is the relational operator, and “b” is operand2.
  • The relational operator “>” checks whether 15 is greater than 10. Since the condition is true, the result returned is true.

Types of Relational Operators in Java

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

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 the left side of operator is greater than the value on the 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 whether the value of left and right operands is 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 the 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.

Java Comparison Operators Examples


Let’s take some important example programs based on all six types of Java relational operators.

Example 1: Using Relational Operator with If Statement

public class IfExample {
  public static void main(String[] args) {
    int number = 15;
    if(number > 10){
       System.out.println("Number is greater than 10");
    }
  }
}

Output:

Number is greater than 10

This Java program demonstrates the use of the if statement along with a relational operator to check a condition and execute a block of code. In this example program:

  • We have declared an integer variable named number and assigned the value 15.
  • We have used this variable in the condition of the if statement.
  • The conditional statement if(number > 10) checks whether number is greater than 10.
  • Since the condition is true, the code inside the if block will execute.

Example 2: Comparing Two Numbers Using Relational Operators

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 or equal to x: " +(y >= x));
  System.out.println("x is less than or 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 or equal to x: true 
 x is less than or equal to y: true 
 x is equal to y: false 
 (x+20 < y+10): true

In this example:

  • The value of a relational expression is either true or false. If the condition is true, it returns true; otherwise, it returns false.
  • In the expression (x + 20 < y + 10), the arithmetic expressions (x + 20) and (y + 10) are evaluated first.
  • After that, the results are compared. This happens because arithmetic operators have higher precedence (priority) than relational operators.

Example 3: Using Relational Operators in Loops

public class LoopExample {
   public static void main(String[] args) {
     for(int i = 1; i <= 5; i++){
        System.out.println(i);
     }
   }
}

Output:

1
2
3
4
5

This Java program demonstrates the use of a relational operator in the for loop to print numbers from 1 to 5.

Let us consider another example program where we will use relational operators in the if-else statement to compare between two numeric values.

Example 4: Compare Two Numbers Using Relational Operators

public class CompareNumbers {
  public static void main(String[] args) {
     int a = 25;
     int b = 40;
     System.out.println("a = " + a + " b = " + b);
     if(a > b)
     {
         System.out.println("a is greater than b");
     }
     else if(a < b)
     {
         System.out.println("a is less than b");
     }
     else
     {
         System.out.println("a is equal to b");
     }
  }
}

Output:

a = 25 b = 40
a is less than b

Example 5: Student Grading System (Real-Time Example)

public class GradeSystem {
   public static void main(String[] args) {
     int marks = 75;
     if(marks >= 90){
          System.out.println("Grade A");
     }
     else if(marks >= 70){
          System.out.println("Grade B");
     }
     else if(marks >= 50){
          System.out.println("Grade C");
     }
     else{
         System.out.println("Fail");
     }
  }
}

Output:

Grade B

In this example:

  • We have declared an integer variable named “marks” and assigned the value 75.
  • This variable stores the marks obtained by a student.
  • The first condition, if(marks >= 90), checks whether the marks are greater than or equal to 90. If this condition is true, the program prints “Grade A”.
  • The second condition, else if(marks >= 70), checks whether the marks are greater than or equal to 70.
  • Since the marks are 75, this condition becomes true, and the program prints “Grade B”.
  • The third condition, else if(marks >= 50), checks whether the marks are greater than or equal to 50. This condition executes only if the previous conditions are false.
  • The else block executes if none of the above conditions are true. It prints “Fail”, meaning the student scored less than 50 marks.
  • Note that the program checks conditions from top to bottom. As soon as one condition becomes true, the remaining conditions are skipped.

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


Key Points About Relational Operators

  • Relational operators in Java are the most frequently used operators in the expressions that control the conditional statements and loop statements.
  • These operators are mainly used to determine equality and the relative order between two values or expressions.
  • Java provides six relational operators: <, >, <=, >=, ==, and !=.
  • The result of a relational expression is always of boolean type, meaning it returns either true or false.
  • Equality in Java is represented by two equal signs (==), not one. A single equal sign (=) is used as an assignment operator.
  • In Java, integers, floating-point numbers, characters, and boolean values can be compared using the equality operator (==) and the inequality operator (!=).
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.