Comparison operators are used to comparing two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values.
They are used with a loop statement and also with if-else statements.
Following are the Relational operators in Java:
Operator | Name | Example |
---|---|---|
== | Equal to. | A == B |
!= | Not equal. | A != B |
> | Greater than. | A > B |
< | Less than. | A < B |
>= | Greater than or equal to. | A >= B |
<= | Less than or equal to. | A <= B |
Java Program for Relational Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Relational { public static void main(String args[]) { int x = 10; int y = 20; System.out.println("Is x == y = " + (x == y) ); System.out.println("Is x != y = " + (x != y) ); System.out.println("Is x > y = " + (x > y) ); System.out.println("Is x < y = " + (x < y) ); System.out.println("Is y >= a = " + (y >= x) ); System.out.println("Is y <= a = " + (y <= x) ); //Demonstration to use it with if-else if(x==y){ System.out.println("X is equal to y"); } else{ System.out.println("X is not equal to y" ); } } } |
Output:
1 2 3 4 5 6 7 | Is x == y = false Is x != y = true Is x > y = false Is x < y = true Is y >= a = true Is y <= a = false X is not equal to y |