Relational Operators in Java are used to compare two values and determine their relationship, resulting in either true or false based on the comparison.
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
1. Equal to (==): The equal to operator checks if two values are equal.
Example:
int a = 25; int b = 30; boolean result = (a == b);
The comparison “a == b” checks if the value of ‘a’ (25) is equal to the value of ‘b’ (30). Since 25 is not equal to 30, the result of the comparison is false.
2. Not equal to (!=): The not equal to operator checks if two values are not equal.
Example:
int a = 25; int b = 30; boolean result = (a != b);
The comparison a != b checks if the value of ‘a’ (25) is not equal to the value of ‘b’ (30). In this case, it would be true because 25 is not equal to 30.
3. Greater than (>): The greater than operator checks if the left operand is greater than the right operand.
Example:
int a = 25; int b = 30; boolean result = (a > b);
The comparison a > b checks if the value of ‘a’ (25) is greater than the value of ‘b’ (30). In this case, it would be false because 25 is not greater than 30.
4. Less than (<): The less than operator checks if the left operand is less than the right operand.
Example:
int a = 25; int b = 30; boolean result = (a < b);
The comparison a < b checks if the value of ‘a’ (25) is less than the value of ‘b’ (30). In this case, it would be true because 25 is less than 30.
5. Greater than or equal to (>=): The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.
Example:
int a = 25; int b = 30; boolean result = (a >= b);
Explanation: The comparison a >= b checks if the value of ‘a’ (25) is greater than or equal to the value of ‘b’ (30). In this case, it would be false because 25 is not greater than or equal to 30.
6. Less than or equal to (<=): The less than or equal to operator checks if the left operand is less than or equal to the right operand.
Example:
int a = 25; int b = 30; boolean result = (a <= b);
The comparison a <= b checks if the value of ‘a’ (25) is less than or equal to the value of ‘b’ (30). In this case, it would be true because 25 is less than or equal to 30.
Here is the source code of the Java Program to Illustrate Use of Relational Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/*
* Java Program to Illustrate Use of Relational Operators
*/
import java.util.Scanner;
public class Relational_Operators
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.print("Enter first integer:");
int a = s.nextInt();
System.out.print("Enter second integer:");
int b = s.nextInt();
System.out.println("a == b : " + (a == b) );
System.out.println("a != b : " + (a != b) );
System.out.println("a > b : " + (a > b) );
System.out.println("a < b : " + (a < b) );
System.out.println("b >= a : " + (b >= a) );
System.out.println("b <= a : " + (b <= a) );
}
}
1. The program imports the Scanner class from the java.util package to read user input.
2. It defines a class named “Relational_Operators” with the main method.
3. The program prompts the user to enter two integers and reads the values using the Scanner object.
4. It compares the entered integers using relational operators and prints the results.
$ javac Relational_Operators.java $ java Relational_Operators Enter first integer:25 Enter second integer:30 a == b : false a != b : true a > b : false a < b : true b >= a : true b <= a : false
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Practice Information Technology MCQs
- Check Java Books
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship