# Relational Operators in Java

Relational Operators in Java are used to compare two values and determine their relationship, resulting in either true or false based on the comparison.

List of Relational Operators in Java:
1. Equal to (==)
2. Not equal to (!=)
3. Greater than (>)
4. Less than (<)
5. Greater than or equal to (>=)
6. 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:

Note: Join free Sanfoundry classes at Telegram or Youtube
```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.

Program/Source Code

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.

1. `/*`
2. ` * Java Program to Illustrate Use of Relational Operators`
3. ` */`
4. `import java.util.Scanner;`
5. `public class Relational_Operators `
6. `{`
7. `    public static void main(String args[]) `
8. `    {`
9. `        Scanner s= new Scanner(System.in);`
10. `        System.out.print("Enter first integer:");`
11. `        int a = s.nextInt();`
12. `        System.out.print("Enter second integer:");`
13. `        int b = s.nextInt();`
14. `        System.out.println("a == b : " + (a == b) );`
15. `        System.out.println("a != b : " + (a != b) );`
16. `        System.out.println("a > b : " + (a > b) );`
17. `        System.out.println("a < b : " + (a < b) );`
18. `        System.out.println("b >= a : " + (b >= a) );`
19. `        System.out.println("b <= a : " + (b <= a) );`
20. `    }`
21. `}`
Program Explanation

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.

Program Output:
```\$ 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”.

If you find any mistake above, kindly email to [email protected]