Relational Operators in Java

The Relational operators in Java programming are mostly used either in If Conditions or Loops. Java Relational operators are commonly used to check the relationship between two variables. If the relation is true, then it will return Boolean True. And if the relation is false, then it will return Boolean False.

The table below shows all the Relational Operators in Java Programming with examples.

Relational Operators in JavaUsageDescriptionExample
>a > ba is greater than b5 > 2 returns True
<a < ba is less than b5 < 2 returns False
>=a >= ba is greater than or equal to b5 >= 2 returns True
<=a <= ba is less than or equal to b5 <= 2 return False
==a == ba is equal to b5 == 2 returns False
!=a != ba is not equal to b5 != 2 returns True

Relational Operators in Java Example

This example helps to understand the Java Relational Operators practically.

This Java program allows the user to enter two integer variables a and b. Next, we use these two variables to perform various relational operations present in Java Programming Language.

// Relational Operators in Java

package JavaOperators;

import java.util.Scanner;

public class RelationalOperators {
	private static Scanner sc;
	public static void main(String[] args) {
		int a, b;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter two integer Value: ");
		a = sc.nextInt();
		b = sc.nextInt();
		
		System.out.println(" Result of a >  b is = " + (a > b));
		System.out.println(" Result of a >=  b is = " + (a >= b));
		System.out.println(" Result of a <  b is = " + (a < b));
		System.out.println(" Result of a <=  b is = " + (a <= b));
		System.out.println(" Result of a ==  b is = " + (a == b));
		System.out.println(" Result of a !=  b is = " + (a != b));
	}
}
Relational Operators in Java 1

Within this relational operators example, the following statement will ask the user to enter integer values a, b. Next, we are going to assign the user input values to variables.

System.out.println(" Please Enter two integer Value: ");
a = sc.nextInt();
b = sc.nextInt();

In the next line, We checked those values against every relational operator we have

Using Java Relational Operators in If Condition

This example will help to understand how relational operators in Java Programming used in If Condition.

For this example, We are using two variables a and b inside the If Statement along with relational operator to perform condition check.

// Using Java Relational Operators in If Statement

package JavaOperators;

import java.util.Scanner;

public class RelationalOperatorsinIf {
	private static Scanner sc;
	public static void main(String[] args) {
		int a, b;
		sc = new Scanner(System.in);
		System.out.println("Please Enter two integer Value: ");
		a = sc.nextInt();
		b = sc.nextInt();
		
		if (a == b) {
			System.out.println("a is equal to b");
		}
		else {
			System.out.println("a is NOT equal to b");
		}
	}
}

From the Java program output, you can observe that we entered a = 22 and b = 65. It means a is not equal to b. So, the second Java statement printed.

Please Enter two integer Value: 
22
65
a is NOT equal to b

Let us see what will happen when we change our values. From the screenshot below, see that we entered a = 12 and b = 12. It means a is equal to b. So, the First statement printed.

Please Enter two integer Value: 
12
12
a is equal to b

In this relational operator example, the following statement will ask the user to enter integer values a, b, and they will assign the user input values to respected variables.

System.out.println(" Please Enter two integer Value: ");
a = sc.nextInt();
b = sc.nextInt();

Relational Operators If Condition

If x is equal to y, then statement inside the If condition block executed.

System.out.println("a is equal to b");

If x is not equal to y, then statement inside the If condition block executed.

System.out.println("a is NOT equal to b");