In this tutorial, you will learn how Java relational operators compare values, which six comparison operators Java provides, what boolean result each operator returns, and where these operators are commonly used in conditions.
What Java relational operators compare and return
Relational operators in Java compare two operands and return a boolean value: either true or false. Because they compare values, relational operators are also called comparison operators.
You usually use Java relational operators inside if statements, while loops, for loop conditions, validation logic, and ternary expressions. For example, the expression age >= 18 checks whether the value stored in age is greater than or equal to 18.
Java supports the following six relational operators.
- Equal-to operator:
== - Not equal-to operator:
!= - Greater-than operator:
> - Less-than operator:
< - Greater-than or equal-to operator:
>= - Less-than or equal-to operator:
<=
Java relational operator symbols, examples, and boolean results
The following table shows the Java relational operator symbol, a simple expression, and the condition that makes the expression return true.
| Java Relational Operator | Operator Symbol | Example Expression | When the Result is true |
|---|---|---|---|
| Equal to | == | a == b | Returns true if a is equal to b. |
| Not equal to | != | a != b | Returns true if a is not equal to b. |
| Greater than | > | a > b | Returns true if a is greater than b. |
| Less than | < | a < b | Returns true if a is less than b. |
| Greater than or equal to | >= | a >= b | Returns true if a is greater than or equal to b. |
| Less than or equal to | <= | a <= b | Returns true if a is less than or equal to b. |
Java relational operators with integer values: complete example
In the following program, we take two integer values in a and b, and demonstrate each of the Relational Operators by competing the values in a and b.
Main.java
public class Main {
public static void main(String[] args) {
int a = 12;
int b = 10;
System.out.println("a == b : " + a + "==" + b + " : " + (a == b));
System.out.println("a != b : " + a + "!=" + b + " : " + (a != b));
System.out.println("a > b : " + a + ">" + b + " : " + (a > b));
System.out.println("a < b : " + a + "<" + b + " : " + (a < b));
System.out.println("a >=b : " + a + ">=" + b + " : " + (a >= b));
System.out.println("a <= b : " + a + "<=" + b + " : " + (a <= b));
}
}
Output
a == b : 12==10 : false
a != b : 12!=10 : true
a > b : 12>10 : true
a < b : 12<10 : false
a >=b : 12>=10 : true
a <= b : 12<=10 : false
Since a is 12 and b is 10, expressions such as a > b and a >= b return true. Expressions such as a == b, a < b, and a <= b return false.
Using Java relational operator results in if, while, and ternary conditions
A relational operator expression can be used anywhere Java expects a boolean condition. The comparison is evaluated first, and then Java uses the resulting true or false value to decide the next step.
public class Main {
public static void main(String[] args) {
int score = 72;
int count = 0;
int age = 20;
if (score >= 50) {
System.out.println("Passed");
}
while (count < 3) {
System.out.println("count = " + count);
count++;
}
String category = age >= 18 ? "Adult" : "Minor";
System.out.println(category);
}
}
Here, score >= 50 controls the if statement, count < 3 controls the while loop, and age >= 18 decides which value the ternary expression assigns to category.
Rules for Java relational operators with numbers, characters, booleans, strings, and objects
The equality operators == and != can be used with primitive values such as numbers, characters, and booleans. The ordering operators >, <, >=, and <= are used with numeric values and compatible primitive numeric types. A char value can also be compared because it has a numeric Unicode value.
For objects, == and != compare references, not object content. This is an important point when working with String values. Use equals() when you want to compare the actual text stored in two strings.
public class Main {
public static void main(String[] args) {
String first = new String("Java");
String second = new String("Java");
System.out.println(first == second);
System.out.println(first.equals(second));
}
}
false
true
The first result is false because first and second refer to two different objects. The second result is true because equals() compares the string content.
Difference between Java relational operators and logical operators
&&, ||, and ! are logical operators, not relational operators. Relational operators create boolean results by comparing values. Logical operators combine or invert boolean results.
int age = 25;
boolean hasId = true;
if (age >= 18 && hasId) {
System.out.println("Allowed");
}
In this example, age >= 18 is a relational expression. The && operator combines that result with the value stored in hasId.
Using relational comparisons instead of switch cases in Java
Java switch cases are not written as relational comparisons such as case marks >= 90. When your logic depends on ranges, use if, else if, and else.
int marks = 86;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Needs improvement");
}
Common mistakes with Java relational operators
- Using assignment
=when equality comparison==is required. - Using
==to compare the content of twoStringobjects instead of usingequals(). - Writing chained comparisons such as
10 < age < 20, which is not valid Java syntax. - Confusing logical operators such as
&&with relational operators such as>=. - Expecting exact equality with floating-point values when the calculation may contain rounding differences.
// Invalid in Java
if (10 < age < 20) {
System.out.println("In range");
}
// Valid in Java
if (age > 10 && age < 20) {
System.out.println("In range");
}
Detailed tutorials for each Java relational operator
The following tutorials provide a well detailed explanation and examples for each of the Relational Operators in Java.
- Java Equal To
- Java Not Equal
- Java Greater Than
- Java Less Than
- Java Greater Than or Equal To
- Java Less Than or Equal To
Java relational operators practice checklist
- Confirm that each relational expression returns a
booleanvalue. - Use
==and!=for equality checks, and use>,<,>=, and<=for ordering checks. - Use
equals()when comparing string content. - Use logical operators such as
&&only to combine boolean conditions, not as a replacement for relational operators. - Rewrite range checks as two comparisons, such as
age > 10 && age < 20.
FAQs on Java relational operators
How many relational operators are there in Java?
Java has six relational operators: ==, !=, >, <, >=, and <=. Each one returns a boolean result.
What are the five relational operators people usually ask about?
Some references informally ask for five relational operators, but Java commonly lists six comparison operators. The complete Java set includes equality, inequality, greater than, less than, greater than or equal to, and less than or equal to.
Is && a relational operator in Java?
No. && is a logical AND operator. It combines two boolean expressions, such as age >= 18 && hasId.
Can Java relational operators compare String values?
== and != can compare whether two string references point to the same object, but they do not compare string content. Use equals() to compare the text stored in two strings.
Can relational operators be used directly in Java switch cases?
For ordinary range checks, use if and else if. A switch case is not written as case marks >= 90.
Reference for Java comparison and operator behavior
For the official Java language tutorial summary of equality, relational, and conditional operators, refer to the Oracle Java Tutorials page on equality, relational, and conditional operators and the Oracle Java Tutorials operator summary.
TutorialKart.com