
Role of Relational Operators in Java: Operators are the foundational elements of any programming language, and Java is no exception. Java offers operators tailored to various tasks, including arithmetic, logical, and relational operations. In this article, we'll explore the world of Java's relational operators, essential for comparing values and making decisions in your programs.
Also read: Top 10 Java Libraries Every Java Developer Should Know
Relational operators, often called comparison operators, are binary operators that allow you to determine the relationships between two values. These operators evaluate the expressions on both sides and return a boolean result, either true or false, based on the comparison's outcome. Java provides several relational operators to facilitate these comparisons.
The equal to operator checks whether two operands are equal. It returns true if the value on the left-hand side is equal to the value on the right-hand side; otherwise, it returns false.
Syntax:
java
variable1 == variable2
Example:
java
int var1 = 5;
int var2 = 10;
int var3 = 5;
System.out.println("var1 == var2: " + (var1 == var2)); // Outputs false
System.out.println("var1 == var3: " + (var1 == var3)); // Outputs true
The not equal operator checks whether two operands are not equal. It returns true if the value on the left-hand side is not equal to the value on the right-hand side; otherwise, it returns false.
Syntax:
java
variable1 != variable2
Example:
java
int var1 = 5;
int var2 = 10;
int var3 = 5;
System.out.println("var1 != var2: " + (var1 != var2)); // Outputs true
System.out.println("var1 != var3: " + (var1 != var3)); // Outputs false
Also read: Java or Advanced Java: Which One Is Right For You in 2026?
The greater than operator checks whether the value on the left-hand side is greater than on the right-hand side. It returns true if the left-hand value is greater; otherwise, it returns false.
Syntax:
java
variable1 > variable2
Example:
java
int var1 = 30;
int var2 = 20;
System.out.println("var1 > var2: " + (var1 > var2)); // Outputs true
The less-than operator checks whether the value on the left side is less than on the right. It returns true if the left-hand value is less; otherwise, it returns false.
Syntax:
java
variable1 < variable2
Example:
java
int var1 = 10;
int var2 = 20;
System.out.println("var1 < var2: " + (var1 < var2)); // Outputs true
The greater than or equal to operator checks whether the value on the left-hand side is greater than or equal to the value on the right-hand side. It returns true if the left-hand value is greater or equal; otherwise, it returns false.
Syntax:
java
variable1 >= variable2
Example:
java
int var1 = 20;
int var2 = 20;
int var3 = 10;
System.out.println("var1 >= var2: " + (var1 >= var2)); // Outputs true
System.out.println("var2 >= var3: " + (var2 >= var3)); // Outputs true
The less than or equal to operator checks whether the value on the left-hand side is less than or equal on the right-hand side. It returns true if the left-hand value is less or equal; otherwise, it returns false.
Syntax:
java
variable1 <= variable2
Example:
java
int var1 = 10;
int var2 = 10;
int var3 = 9;
System.out.println("var1 <= var2: " + (var1 <= var2)); // Outputs true
System.out.println("var2 <= var3: " + (var2 <= var3)); // Outputs false
Also read: Top 15 Java Projects With Source Code [2024]