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.
- Relational operators are essential for implementing decision-making and branching logic.
- They return a boolean value (true or false) based on the comparison result.
- These operators are commonly used in if, if-else, switch, while, for, and other control flow statements.
public class Geeks{
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a < b);
}
}
Output
true
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.
- Returns true when both operands have equal values.
- Commonly used in conditional statements for comparison.
Syntax:
operand1 == operand2
// 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.
- Returns true when the operands are not equal.
- Frequently used to validate conditions and inputs.
Syntax:
var1 != var2
// 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.
- Returns true if the left operand is greater.
- Useful for comparing numeric values.
Syntax:
var1 > var2
// 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.
- Returns true if the left operand is less than the right operand.
- Often used in loops and conditional expressions.
Syntax:
var1 < var2
// 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.
- Returns true if the left operand is greater than or equal to the right operand.
- Useful when equality should also satisfy the condition.
Syntax:
var1 >= var2
// 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.
- Returns true if the left operand is less than or equal to the right operand.
- Commonly used for range checking and loop conditions.
Syntax:
var1 <= var2
// 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:
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));
}
}
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
- Easy to understand: Relational operators are simple and easy to understand, making it easy to write code that performs comparisons.
- Versatile: Relational operators can be used to compare values of different data types, such as integers, floating-point numbers, and strings.
- Essential for making decisions: Relational operators are essential for making decisions in a program, as they allow you to control the flow of a program based on the results of comparisons.
- Efficient: Relational operators are efficient, as they perform comparisons quickly and accurately.
- Reusable code: The code that uses relational operators can be reused in different parts of a program, making it easier to maintain and update the code.
- Improved code readability: By using relational operators, you can make your code more readable and understandable, as the comparisons are clearly stated in the code.
- Debugging made easier: Relational operators make debugging easier, as you can use them to check the values of variables and to find out where a problem is occurring in your code.