Relational operators in Java are symbols used to compare between two values (i.e. operands) or expressions. These comparisons determine the relationship between the operands and always return a boolean value either true or false. Relational operators are commonly used in:
- if and else conditions
- while or for loops
- Logical expressions
- Comparisons in business logic
List of Relational Operators in Java
There are six relational operators in Java programming which is shown in the below table.
Operator | Description | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Syntax of Relational Operators in Java
The basic syntax for a relational operator in Java is as:
operand1 operator operand2
Where:
operand1
andoperand2
are values, variables, or expressions to compare.operator
is one of the relational operators:==
,!=
,>
,<
,>=
, or<=
.
Basic Examples of Relational Operators
1. Equal To (==
)
The purpose of this operator is to check if two values are equal or not.
Example 1:
public class EqualToExample {
public static void main(String[] args) {
int a = 10;
int b = 10;
System.out.println("a == b : " + (a == b));
}
}
Output:
a == b : true
2. Not Equal To (!=
)
The not equal to (!=) operator is a relational operator which checks if two values are not equal. This operator returns true if the values are different. Otherwise, it return false if the values are equal.
Example 2:
public class NotEqualToExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a != b : " + (a != b));
}
}
Output:
a != b : true
Since 10
is not equal to 20
, the result is true
.
Note: The not equal to operator works with primitive types like int, float, char, boolean, etc. When you will compare objects like String using this operator, the != operator compares reference (i.e. memory address), not actual content.
Example 3: String Comparison using !=
and .equals()
public class StringComparison {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println("Using != (reference comparison):");
System.out.println("s1 != s2 : " + (s1 != s2)); // true because s1 and s2 are different objects.
System.out.println("\nUsing equals() (value comparison):");
System.out.println("s1.equals(s2) : " + s1.equals(s2)); // true because the contents are the same.
}
}
Output:
Using != (reference comparison):
s1 != s2 : true
Using equals() (value comparison):
s1.equals(s2) : true
In this example, the statement s1 != s2 compares object references. Since both are created with new String(), they point to different memory locations even though they have the same content. The statement s1.equals(s2) compares the actual content of the strings, which is “Java” in both cases — so it returns true.
3. Greater Than (>
)
The greater than (>) operator is a relational operator in Java which is used to check if the left operand is strictly greater than the right operand. It returns true value if the left operand is greater than the right. Otherwise, it returns false value.
Example 4:
public class GreaterThanBasic {
public static void main(String[] args) {
int a = 20;
int b = 10;
System.out.println("a > b : " + (a > b)); // true
}
}
Output:
a > b : true
Example 5: with if
Condition
public class GreaterThanIf {
public static void main(String[] args) {
int age = 21;
if (age > 18) {
System.out.println("You are eligible to vote.");
}
}
}
Output:
You are eligible to vote.
Characters in Java are compared based on their Unicode values (ASCII for basic characters). Let’s take an example on it.
Example 6:
public class GreaterThanChar {
public static void main(String[] args) {
char ch1 = 'D';
char ch2 = 'A';
System.out.println("ch1 > ch2 : " + (ch1 > ch2)); // true (because 'D' > 'A')
}
}
Output:
ch1 > ch2 : true
4. Less Than (<
)
The less than operator (<) is a relational operator in Java used to check if the value on the left is strictly less than the value on the right. It returns a boolean value true if the left value is less than the right. Otherwise, this operator returns false.
Example 7:
public class LessThanBasic {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a < b : " + (a < b)); // true
}
}
Output:
a < b : true
Example 8:
public class LessThanIf {
public static void main(String[] args) {
int temperature = 35;
if (temperature < 40) {
System.out.println("Safe temperature.");
}
}
}
Output:
Safe temperature.
5. Greater Than or Equal To (>=
)
The greater than or equal to (>=) is a relational operator in Java which is used to check if the left operand is greater than or equal to the right operand. This operator returns true boolean value if the left value is greater than or equal to the right. Otherwise, it returns false.
Example 9:
public class GreaterThanOrEqualExample {
public static void main(String[] args) {
int a = 15;
int b = 15;
System.out.println("a >= b : " + (a >= b));
}
}
Output:
a >= b : true
6. Less Than or Equal To (<=)
The relational operator less than or equal to (<=) checks if the left operand is less than or equal to the right operand. This operator returns true value if the left operand is less than or equal to the right operand. This operator returns true boolean value if the left value is less than or equal to the right. Otherwise, it returns false.
Example 10:
public class LessThanOrEqualExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a <= b : " + (a <= b));
}
}
Output:
a <= b : true
Java Program: Demonstrating All Relational Operators
Example 11:
public class RelationalOperatorsDemo {
public static void main(String[] args) {
// Basic integer comparison
int a = 10;
int b = 20;
System.out.println("Basic Integer Comparisons:");
System.out.println("a = " + a + ", b = " + b);
System.out.println("a == b : " + (a == b)); // false
System.out.println("a != b : " + (a != b)); // true
System.out.println("a > b : " + (a > b)); // false
System.out.println("a < b : " + (a < b)); // true
System.out.println("a >= b : " + (a >= b)); // false
System.out.println("a <= b : " + (a <= b)); // true
System.out.println("\nFloating-point Comparison:");
double x = 15.75;
double y = 15.75;
System.out.println("x = " + x + ", y = " + y);
System.out.println("x == y : " + (x == y)); // true
System.out.println("x != y : " + (x != y)); // false
System.out.println("x >= y : " + (x >= y)); // true
System.out.println("\nCharacter Comparison:");
char ch1 = 'A';
char ch2 = 'B';
System.out.println("ch1 = " + ch1 + ", ch2 = " + ch2);
System.out.println("ch1 < ch2 : " + (ch1 < ch2)); // true (ASCII comparison)
System.out.println("ch1 == ch2 : " + (ch1 == ch2)); // false
System.out.println("\nBoolean Comparison:");
boolean bool1 = true;
boolean bool2 = false;
System.out.println("bool1 == bool2 : " + (bool1 == bool2)); // false
System.out.println("bool1 != bool2 : " + (bool1 != bool2)); // true
}
}
Output:
Basic Integer Comparisons:
a = 10, b = 20
a == b : false
a != b : true
a > b : false
a < b : true
a >= b : false
a <= b : true
Floating-point Comparison:
x = 15.75, y = 15.75
x == y : true
x != y : false
x >= y : true
Character Comparison:
ch1 = A, ch2 = B
ch1 < ch2 : true
ch1 == ch2 : false
Boolean Comparison:
bool1 == bool2 : false
bool1 != bool2 : true