Comparison operators in Java are used to compare two values or expressions. These operators return a boolean value (either true or false) based on the comparison.
Comparison Operators
Common comparison operators are:
== : Checks if two values are equal.
!= : Checks if two values are not equal.
< : Checks if the left value is less than the right value.
<= : Checks if the left value is less than or equal to the right value.
> : Checks if the left value is greater than the right value.
>= : Checks if the left value is greater than or equal to the right value.
Syntax Example:
int a = 5, b = 10;
boolean result = (a == b); // false
result = (a != b); // true
result = (a < b); // true
result = (a <= b); // true
result = (a > b); // false
result = (a >= b); // false
Program
INPUT
public class ComparisonOperatorsExample {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
// Comparison using ==
System.out.println("Is num1 equal to num2? " + (num1 == num2));
// Comparison using !=
System.out.println("Is num1 not equal to num2? " + (num1 != num2));
// Comparison using <
System.out.println("Is num1 less than num2? " + (num1 < num2));
// Comparison using <=
System.out.println("Is num1 less than or equal to num2? " + (num1 <= num2));
// Comparison using >
System.out.println("Is num1 greater than num2? " + (num1 > num2));
// Comparison using >=
System.out.println("Is num1 greater than or equal to num2? " + (num1 >= num2));
}
}