Blog

Relational Operators

This operator uses to compare both the operands operands and validate the relationship between them

There are six different types of relational operators. Let us discuss below:

==: This operator compares both the operands are equal or not, if equal it returns true else it returns false

!=: This operator compares both the operands are not equal, if not equals it returns true else it returns false

>=: This operator compares whether one operand value is greater than or equal to the other operand

<=: This operator compares whether one operand value is greater than or equal to the other operand

>: This operator compares whether left operand is greater than right operand

<: This operator compares whether left operand is less than right operand

The below example compares x, y and z and returns true or false based on the operational

package com.test.basics;

public class TestRelationalOperator {
	public static void main(String[] args) {

		int x = 5;
		int y = 5;
		int z = 10;

		System.out.println("Comparing x and y with equal values using = operator " + (x == y));
		System.out.println("Comparing x and z with inequal values using == operator " + (x == z));

		System.out.println("Comparing x and y values not equals using != operator " + !(x == y));
		System.out.println("Comparing x and z values not equals using != operator " + !(x == z));

		System.out.println("Comparing x and y with greater than or equal to using >= operator " + (x >= y));
		System.out.println("Comparing x and z with greater than or equal to >= operator " + (x >= z));

		System.out.println("Comparing x and y with less than or equal to using = operator " + (x <= y));
		System.out.println("Comparing x and z with less than or equal to using == operator " + (x <= z));

		System.out.println("Comparing z and y with greater then using > operator " + (z > y));
		System.out.println("Comparing x and z with greater then using > operator " + (x > z));

		System.out.println("Comparing z and y with less then using < operator " + (z < y));
		System.out.println("Comparing x and z with less then using < operator " + (x < z));

	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *