![]() |
VOOZH | about |
Relational Operators in Java are used to evaluate the relationship between two operands by comparing their values. They help determine whether a specific condition is satisfied and produce a boolean result that can be used to control program execution.
true
Explanation: The < operator checks whether a is less than b; since 10 < 20, the result is true
==)The Equal To (==) operator is used to check whether two operands have the same value. It returns true if both operands are equal; otherwise, it returns false.
Syntax:
operand1 == operand2
Var1 = 5 Var2 = 10 Var3 = 5 var1 == var2: false var1 == var3: true
!=)The Not Equal To (!=) operator checks whether two operands have different values. It works opposite to the equal-to operator.
Syntax:
var1 != var2
Var1 = 5 Var2 = 10 Var3 = 5 var1 != var2: true var1 != var3: false
>)The Greater Than (>) operator checks whether the left operand is greater than the right operand.
Syntax:
var1 > var2
Var1 = 30 Var2 = 20 Var3 = 5 var1 > var2: true var3 > var1: false
<)The Less Than (<) operator checks whether the left operand is smaller than the right operand.
Syntax:
var1 < var2
Var1 = 10 Var2 = 20 Var3 = 5 var1 < var2: true var2 < var3: false
The Greater Than or Equal To (>=) operator checks whether the left operand is either greater than or equal to the right operand.
Syntax:
var1 >= var2
Var1 = 20 Var2 = 20 Var3 = 10 var1 >= var2: true var2 >= var3: true
The Less Than or Equal To (<=) operator checks whether the left operand is either less than or equal to the right operand.
Syntax:
var1 <= var2
Var1 = 10 Var2 = 10 Var3 = 9 var1 <= var2: true var2 <= var3: false
Example: Program that implements all relational operators in Java for user input:
num1 > num2 is false num1 < num2 is true num1 >= num2 is false num1 <= num2 is true num1 == num2 is false num1 != num2 is true
Explanation This program demonstrates the use of all relational operators in Java. It reads two numbers using the Scanner class and compares them using operators such as >, <, >=, <=, ==, and !=. The results of these comparisons are displayed using System.out.println(). Since relational operators return boolean values (true or false), the program helps users understand how different comparisons work in Java.