Relational Operators In Java

Comparison operators | Relational Operators

Well, as the name suggests, these operators are going to be used to perform comparisons between the data. When it comes to comparison, we are usually doing some comparison like some number is greater than another number, or something like that.

Let’s have a look at what are the comparison operators that we are having here –

Operator Symbol Use
==This operator is used to check equality between operands. For example, if we wish to check whether some number is equal to another number, we can use this thing
!=Contrary to the above operator, this one checks for inequality. For example, if we want to check if some number is not equal to another number, we can use this thing
>=This operator is used to check if something is greater than or equal to another thing. For example, if we want to check if some number is greater than or equal to another number, we can use this thing.
<=This operator is used to check if something is lesser than or equal to another thing. For example, if we want to check if some number is less than or equal to another thing, we can use this operator.
>This operator is used to check if something is greater than the other thing. For example, if we want to check if some number is greater than another number, then we can use this operator.
<This operator is used to check if something is lesser than the other thing. For example, if we want to check if some number is less than another number, we can use this operator

We need to remember that these operators are used for comparison, and they are going to get us outputs in true or false. So, we can use these operators as per our requirements.

Relational Operators In Java

Let’s have a look at the below program, to understand the comparison or relational operators.

public class Main{
public static void main(String[] args) {
System.out.println(3==3); // true
System.out.println(10!=4); // true
System.out.println(3>=6); // false
System.out.println(3<=100); // true
System.out.println(120>34); // true
System.out.println(7654<1212); //false
}
}

If you have a look at the output, the output comes out to be something like the above-stated comments. So, as you can see, we are able to compare the values here (well, I had put the values directly, but when we are going to use them practically, we will be often making use of the variables). You can use comparison operators as and when required.