The operators in Java that compare two numeric values or two quantities are called relational operators. These operators determine the relationship between them by comparing operands and comparing them. Therefore, relational operators are also called comparison operators. These operators are primarily used to make decisions or control the flow of a program. In Java, relational operators are used to compare two values or operands and return a boolean result (true or false) based on the comparison.
Relational operators are commonly used in conditional statements (such as if-else statements, switch), loops, and other situations where comparisons need to be made to control the program’s flow. The result of a relational operation is a boolean value (true or false), indicating whether the specified condition is satisfied or not.
Operator | Description | Example |
== (Equal to) | If the values of two operands are equal, then the condition becomes true. | (A == B) is not true |
!= (Not Equal) | If the values of two operands are not equal, then the condition becomes true. | (A != B) is true |
> (Greater than) | If the value of the left operand is greater than the value of the right operand, then the condition becomes true. | (a > b) is not true |
< (Less than) | If the value of the left operand is less than the value of right operand, then the condition becomes true. | (a < b) is true |
>= (Greater than or equal to) | If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true. | (a >= b) is not true |
<= (Less than or equal to ) | The condition becomes true if the left operand is less than or equal to the right operand. | (a <= b) is true |
Equal to (==):
public class operator { public static void main(String[] args) { int a = 5; int b = 5; boolean result = (a == b); // true, as the value of 'a' (5) is equal to the value of 'b' (5) System.out.println(result); } }
Output: true
Not equal to (!=):
public static void main(String[] args) { int a = 10; int b = 10; boolean result = (a != b); // true, as the value of 'a' (5) is not equal to the value of 'b' (10) System.out.println(result); }
Output: false
Greater than (>):
public static void main(String[] args) { int a = 10; int b = 155; boolean result = (a > b); // true, as the value of 'a' (10) is greater than the value of 'b' (5) System.out.println(result); }
Output: false
Less than (<):
public static void main(String[] args) { int a = 5; int b = 10; boolean result = (a < b); // true, as the value of 'a' (5) is less than the value of 'b' (10) System.out.println(result); }
Output: true
Greater than or equal to (>=):
public static void main(String[] args) { int a = 8; int b = 8; boolean result = (a >= b); // true, as the value of 'a' (5) is less than or equal to the value of 'b' (10) System.out.println(result); }
Output: true
Less than or equal to (<=):
public static void main(String[] args) { int a = 5; int b = 10; boolean result = (a <= b); // true, as the value of 'a' (5) is less than or equal to the value of 'b' (10) System.out.println(result); }
Output: true