In this tutorial, you will learn about Relational Operators, how many Relational Operators are there in Java, what are they, and examples for these operators.
Relational Operators in Java
Relational Operators in Java are those that give information regarding the relation/comparison between two entities/operands. The relation is if they are equal, or greater than other, less than other, etc. Since these operators compare the given operands, they are also called Comparison Operators.
In Java, Relational Operators return boolean value.
Java supports following 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
1. Operator Symbol – Example – Description
The following table gives more information about these relational operators in Java.
Relational Operator | Operator Symbol | Example | Description |
---|---|---|---|
Equal to | == | a == b | Returns true if a is equal to b , else false. |
Not equal to | != | a != b | Returns true if a is not equal to b , else false. |
Greater than | > | a > b | Returns true if a is greater than b , else false. |
Less than | < | a < b | Returns true if a is less than b , else false. |
Greater than or equal to | >= | a >= b | Returns true if a is greater than or equal to b , else false. |
Less than or equal to | <= | a <= b | Returns true if a is less than or equal to b , else false. |
2. Example Program for Relational Operators
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
3. Relational Operators Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Relational Operators in Java.