Java Relational Operators
In Java, relational operators are used to compare two values. They help you check whether one value is greater than, less than, or equal to another.
These operators always return a boolean result — either true or false depending on whether the condition is satisfied. They are mostly used in conditional statements like if, while, and for loops to make logical decisions in a program.
Java allows you to use these operators with primitive data types such as int, float, double, and char.
List of Relational Operators in Java
Operator | Description | Example |
== (equal to) | Checks if two values are equal. | (A == B) returns true if A and B are equal |
!= (not equal to) | Checks if two values are not equal. | (A != B) returns true if A and B are not equal |
| Checks if the left operand is greater than the right. | (A > B) returns true if A > B |
< (less than) | Checks if the left operand is smaller than the right. | (A < B) returns true if A < B |
| Left operand is greater than or equal to right. | (A >= B) returns true if A ≥ B |
<= (less than or equal to) | Left operand is smaller than or equal to right. | (A <= B) returns true if A ≤ B |
Example 1: Equality and Inequality Operators
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b));
System.out.println("a != b = " + (a != b));
}
}a == b = false a != b = true
Example 2: Greater Than and Less Than Operators
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a > b = " + (a > b));
System.out.println("a < b = " + (a < b));
}
}a > b = false a < b = true
Example 3: Greater Than or Equal To and Less Than or Equal To
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("b >= a = " + (b >= a));
System.out.println("b <= a = " + (b <= a));
}
}b >= a = true b <= a = false
Summary Table
Operator | Meaning | Returns True When... |
== | Equal to | Both values are the same |
!= | Not equal to | Values are different |
Greater than | Left operand is greater | |
< | Less than | Left operand is smaller |
| Greater than or equal to | Left operand ≥ right operand |
<= | Less than or equal to | Left operand ≤ right operand |