Relational operators in Java

Relational operators in java language e.g. equal, greater than or less than etc. are used to check the relationship between two variables or expressions. It is also known as comparison operators in java programming.

For example,

If int a =10 and int b =20;
Then, we car write a relation ship as a == b to check if a and b are equal.

Or as , a > = b; to check if a is >= b etc.

NOTE: All Relational operators returns Boolean value. E.g. considering above value of a and b, the expression, a == b, returns false, as a is 10 and b=20.

 

List of relational operators in java language.

= = Equal to, e.g. a == b;

> Greater than e.g. a > b;

< Less that e.g. a < b;

> = Greater than or equal to. E.g. a >= b;

< = Less than or equal to. E.g. a <=b;

!= Not equal to. E.g. a != b;

 

Relational Operators Example in Java

Uses of all relational operators are shown by java program example. All expressions using relational operators returns true or false as an output.

public class Sample {

	public static void main(String[] args) {

		int a = 10;
		int b = 20;

		// Check if a is Equal to b
		System.out.println("a == b :" + (a == b));

		// Check if a is greater than b
		System.out.println("a > b :" + (a > b));

		// Check if a is less than b
		System.out.println("a < b :" + (a < b)); // Check if a is greater than or equal to b System.out.println("a >= b :" + (a >= b));

		// Check if a is less than or equal to b
		System.out.println("a <= b :" + (a <= b));

		// Check if a is not equal to b
		System.out.println("a != b :" + (a != b));

	}
}

Output:
a == b :false
a > b :false
a < b :true a >= b :false
a <= b :true
a != b :true



Leave a Comment