The relational operators are used to determine if one operand is equal to, greater than, less than, or not equal to the other operand with which it is compared. Just like mathematical operators, most of these operators are just like the normal operators we use in the everyday life. But certain operators are used in a different manner also.
For example, in place of “=” operator that we generally use for comparing two operands are equal, the relational operators in the Java programming language use “==’.
The relational operators are defined as binary expressions, while their operands are numeric expressions.
When applying these operators, the rules that apply to the binary operators are applied and their evaluation returns a numeric value. Not only this, the operator precedence of relational operators is higher than assignment operators but lower than arithmetic operators.
The six relational operators used in Java and their uses are listed in the table below:
| Relational Operator | Description |
| Equal to | == |
| Not equal to | != |
| Greater than | > |
| Greater than or equal to | >= |
| Less than | < |
| Less than or equal to | <= |
Next, let’s look at the code for the program demo_of_relational_operator that shows the use of the comparison operators.
class demo_of_relational_operator {
public static void main(String[] args){
int num_var_1 = 32;
int num_var_2 = 47;
if(num_var_1 == num_var_2) System.out.println(“num_var_1 == num_var_2″);
if(num_var_1 != num_var_2) System.out.println(“num_var_1 != num_var_2″);
if(num_var_1 > num_var_2) System.out.println(“num_var_1 > num_var_2″);
if(num_var_1 < num_var_2) System.out.println(“num_var_1 < num_var_2″);
if(num_var_1 <= num_var_2) System.out.println(“num_var_1 <= num_var_2″);
}
}
Output:
num_var_1 != num_var_2
num_var_1 < num_var_2
num_var_1 <= num_var_2
Next, let’s look at another code snipped displaying the use of different relational operators.
public class Demo_of_Relational_Operators
{
public Demo_of_Relational_Operators ( )
{
int num_var_1 = 10, num_var_2 = 5;
System.out.println(”num_var_1 > num_var_2 : “+(num_var_1 > num_var_2 ));
System.out.println(”num_var_1 < num_var_2 : “+(num_var_1 < num_var_2 ));
System.out.println(”num_var_1 >= num_var_2 : “+(num_var_1 >= num_var_2 ));
System.out.println(”num_var_1 <= num_var_2 : “+(num_var_1 <= num_var_2 ));
System.out.println(”num_var_1 == num_var_2 : “+(num_var_1 == num_var_2 ));
System.out.println(”num_var_1 != num_var_2 : “+(num_var_1 != num_var_2 ));
}
public static void main(String args[]){
new Demo_of_Relational_Operators ();
}
}