Java Relational Operators with Examples (original) (raw)

Last Updated : 9 Jun, 2026

Relational Operators in Java are used to evaluate the relationship between two operands by comparing their values. They help determine whether a specific condition is satisfied and produce a boolean result that can be used to control program execution.

public class Geeks{ public static void main(String[] args) { int a = 10; int b = 20;

    System.out.println(a < b);
}

}

`

**Explanation: The < operator checks whether a is less than b; since 10 < 20, the result is true

Tpyes of Relational operators in Java

Equal To Operator (==)

The Equal To (==) operator is used to check whether two operands have the same value. It returns true if both operands are equal; otherwise, it returns false.

**Syntax:

operand1 == operand2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 5, var2 = 10, var3 = 5;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 == var2: "
                       + (var1 == var2));

    // Comparing var1 and var3 and
    // printing corresponding boolean value
    System.out.println("var1 == var3: "
                       + (var1 == var3));
}

}

`

Output

Var1 = 5 Var2 = 10 Var3 = 5 var1 == var2: false var1 == var3: true

Not Equal To Operator (!=)

The Not Equal To (!=) operator checks whether two operands have different values. It works opposite to the equal-to operator.

**Syntax:

var1 != var2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 5, var2 = 10, var3 = 5;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 != var2: "
                       + (var1 != var2));

    // Comparing var1 and var3 and
    // printing corresponding boolean value
    System.out.println("var1 != var3: "
                       + (var1 != var3));
}

}

`

Output

Var1 = 5 Var2 = 10 Var3 = 5 var1 != var2: true var1 != var3: false

Greater Than Operator (>)

The Greater Than (>) operator checks whether the left operand is greater than the right operand.

**Syntax:

var1 > var2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 30, var2 = 20, var3 = 5;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 > var2: " + (var1 > var2));

    // Comparing var1 and var3 and
    // printing corresponding boolean value
    System.out.println("var3 > var1: "
                       + (var3 >= var1));
}

}

`

Output

Var1 = 30 Var2 = 20 Var3 = 5 var1 > var2: true var3 > var1: false

Less Than Operator (<)

The Less Than (<) operator checks whether the left operand is smaller than the right operand.

**Syntax:

var1 < var2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 10, var2 = 20, var3 = 5;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 < var2: " + (var1 < var2));

    // Comparing var2 and var3 and
    // printing corresponding boolean value
    System.out.println("var2 < var3: " + (var2 < var3));
}

}

`

Output

Var1 = 10 Var2 = 20 Var3 = 5 var1 < var2: true var2 < var3: false

Greater Than or Equal To Operator (>=)

The Greater Than or Equal To (>=) operator checks whether the left operand is either greater than or equal to the right operand.

**Syntax:

var1 >= var2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 20, var2 = 20, var3 = 10;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 >= var2: "
                       + (var1 >= var2));

    // Comparing var2 and var3 and
    // printing corresponding boolean value
    System.out.println("var2 >= var3: "
                       + (var2 >= var3));
}

}

`

Output

Var1 = 20 Var2 = 20 Var3 = 10 var1 >= var2: true var2 >= var3: true

Less Than or Equal To Operator (<=)

The Less Than or Equal To (<=) operator checks whether the left operand is either less than or equal to the right operand.

**Syntax:

var1 <= var2

Java `

// Importing I/O classes import java.io.*;

// Main class class GFG {

// Main driver method
public static void main(String[] args)
{
    // Initializing variables
    int var1 = 10, var2 = 10, var3 = 9;

    // Displaying var1, var2, var3
    System.out.println("Var1 = " + var1);
    System.out.println("Var2 = " + var2);
    System.out.println("Var3 = " + var3);

    // Comparing var1 and var2 and
    // printing corresponding boolean value
    System.out.println("var1 <= var2: "
                       + (var1 <= var2));

    // Comparing var2 and var3 and
    // printing corresponding boolean value
    System.out.println("var2 <= var3: "
                       + (var2 <= var3));
}

}

`

Output

Var1 = 10 Var2 = 10 Var3 = 9 var1 <= var2: true var2 <= var3: false

**Example: Program that implements all relational operators in Java for user input:

Java `

import java.util.Scanner;

public class RelationalOperators { public static void main(String[] args) { Scanner scan = new Scanner(System.in);

//System.out.println("Enter first number: ");

// int num1 = scan.nextInt();

// System.out.println("Enter second number: "); // int num2 = scan.nextInt();

int num1 =1;
int num2 = 2;


System.out.println("num1 > num2 is " + (num1 > num2));
System.out.println("num1 < num2 is " + (num1 < num2));
System.out.println("num1 >= num2 is " + (num1 >= num2));
System.out.println("num1 <= num2 is " + (num1 <= num2));
System.out.println("num1 == num2 is " + (num1 == num2));
System.out.println("num1 != num2 is " + (num1 != num2));

} }

`

Try It Yourselfredirect icon

Output

num1 > num2 is false num1 < num2 is true num1 >= num2 is false num1 <= num2 is true num1 == num2 is false num1 != num2 is true

**Explanation This program demonstrates the use of all relational operators in Java. It reads two numbers using the Scanner class and compares them using operators such as >, <, >=, <=, ==, and !=. The results of these comparisons are displayed using System.out.println(). Since relational operators return boolean values (true or false), the program helps users understand how different comparisons work in Java.

Advantages of using relational operators