Relational Operators in Java
Relational operators in Java are used to compare two values or expressions. They check the relationship between values and return a boolean result: either true or false.
These operators are commonly used in decision-making statements such as if, if-else, loops, and conditional expressions in Java programs.
What are Relational Operators in Java?
Relational operators are special symbols that compare two operands. They determine whether one value is equal to, greater than, less than, or different from another value.
For example, the expression 10 > 5 checks whether 10 is greater than 5 and returns true.
Syntax of Relational Operators in Java
The basic syntax of using a relational operator in Java is:
value1 operator value2;
Here, the operator compares the two values and produces a boolean result.
Example of Relational Operator in Java
public class Main {
public static void main(String[] args) {
int number1 =20;
int number2 = 10;
System.out.println(number1 > number2);
}
}
Types of Relational Operators in Java
Java provides six relational operators that are used for comparing values.
- Equal to Operator (==)
- Not Equal to Operator (!=)
- Greater than Operator (>)
- Less than Operator (<)
- Greater than or Equal to Operator (>=)
- Less than or Equal to Operator (<=)
Java Relational Operators Table
| Operator | Name | Description |
|---|---|---|
| == | Equal To | Checks whether two values are equal |
| != | Not Equal To | Checks whether two values are different |
| > | Greater Than | Checks if one value is greater than another |
| < | Less Than | Checks if one value is smaller than another |
| >= | Greater Than or Equal | Checks if value is greater or equal |
| <= | Less Than or Equal | Checks if value is smaller or equal |
Equal To Operator (==) in Java
The equal to operator (==) is used to check whether two values are equal or not. If both values are the same, it returns true; otherwise, it returns false.
int number1 = 10;
int number2 = 10;
System.out.println(number1 == number2);
Not Equal To Operator (!=) in Java
The not equal to operator (!=) checks whether two values are different. It returns true when the values are not equal.
int age1 = 18;
int age2 = 20;
System.out.println(age1 != age2);
Greater Than Operator (>) in Java
The greater than operator (>) checks whether the value on the left side is greater than the value on the right side.
int marks = 85;
int passingMarks = 40;
System.out.println(marks > passingMarks);
Less Than Operator (<) in Java
The less than operator (<) checks whether the left value is smaller than the right value.
int temperature = 25;
int limit = 30;
System.out.println(temperature < limit);
Greater Than or Equal To Operator (>=) in Java
The greater than or equal to operator (>=) checks whether a value is greater than or equal to another value.
int score = 50;
int required = 50;
System.out.println(score >= required);
Less Than or Equal To Operator (<=) in Java
The less than or equal to operator (<=) checks whether a value is smaller than or equal to another value.
int age = 18;
int limit = l18;
System.out.println(age <= limit);
Using Relational Operators with if Statement in Java
Relational operators are commonly used with conditional statements to execute code based on comparisons.
int age = 20;
if (age >= 18) {
System.out.println("Eligible");
}
Equal To Operator (==) in Java
The equal to operator (==) is used to check whether two values are equal or not. If both values are the same, it returns true; otherwise, it returns false.
int 10;
int number2 = 10;
System.out.println(number1 == number2);
Not Equal To Operator (!=) in Java
The not equal to operator (!=) checks whether two values are different. It returns true when the values are not equal.
int age1 = 18;
int age2 = 20;
System.out.println(age1 != age2);
Greater Than Operator (>) in Java
The greater than operator (>) checks whether the value on the left side is greater than the value on the right side.
int marks = 85;
int passingMarks =40;
System.out.println(marks > passingMarks);
Less Than Operator (<) in Java
The less than operator (<) checks whether the left value is smaller than the right value.
int temperature = 25;
int limit = 30;
System.out.println(temperature < limit);
Greater Than or Equal To Operator (>=) in Java
The greater than or equal to operator (>=) checks whether a value is greater than or equal to another value.
int score = 50;
int required = 50;
System.out.println(score >= required);
Less Than or Equal To Operator (<=) in Java
The less than or equal to operator (<=) checks whether a value is smaller than or equal to another value.
int age = 18;
int limit = 18;;
System.out.println(age <= limit);
Using Relational Operators with if Statement in Java
Relational operators are commonly used with conditional statements to execute code based on comparisons.
int age = 20;
if (age >= 18) {
System.out.println("Eligible");
}
Advantages of Relational Operators in Java
Decision Making
Relational operators help programs make decisions using conditions and comparisons.
Easy Comparison
They provide a simple way to compare values and expressions in Java.
Boolean Result
They return true or false values that can be used with conditional statements.
Used in Loops
Relational operators are widely used in loops like for, while, and do-while loops.
Common Mistakes While Using Relational Operators
- Using assignment operator (=) instead of equality operator (==).
- Comparing incompatible data types.
- Forgetting that relational operators return boolean values.
- Using incorrect conditions in decision-making statements.
Difference Between Relational Operators and Assignment Operators
| Relational Operators | Assignment Operators |
|---|---|
| Used to compare two values. | Used to assign values to variables. |
| Returns true or false. | Stores or updates variable values. |
| Example: x > y | Example: x = 10 |
Example of Multiple Relational Operators in Java
int a = 15;
int b = 10;
System.out.println(a > b);
System.out.println(a == b);
System.out.println(a != b);
false
true
Summary
Relational operators in Java are used to compare two values and check their relationship. They return boolean results such as true or false.
Java provides six relational operators: equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
Understanding relational operators is essential because they are used in conditions, loops, and decision-making logic in Java applications.