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
- Not equal to
- Greater than
- Less than
- Greater than or equal to
- Less than or equal to
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. |
Example Program
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
Relational Operators Tutorials
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
Conclusion
In this Java Tutorial, we learned about different Relational Operators in Java Programming with examples.