In our daily life, we often make comparisons like checking who scored more marks, which number is greater or whether two things are equal. These comparisons help us make decisions. Similarly, in Java relational operators help a program compare values and make choices based on the results.
What Are Relational Operators?
Relational operators are symbols used in Java to compare two values or expressions. The result of this comparison is always a boolean value — either true or false. For example:
int a = 10, b = 20;
System.out.println(a < b); // Output: trueHere, the operator < checks if a is less than b. Since 10 is indeed less than 20, the result is true.
Purpose of Relational Operators
The main purpose of relational operators is to compare values and control the flow of execution in decision-making statements like if, while and for. They help programs make logical decisions. For example:
- Checking if a number is positive or negative.
- Comparing two marks to find who scored higher.
- Ensuring that user inputs match certain conditions.
Why Relational Operators Matter in Java?
Relational operators play a key role in conditional programming. They form the foundation of control structures such as if-else, loops, and switch. Without these comparisons, a program would just execute commands sequentially without logic or decision-making.
Types of Relational Operators in Java
- Equal To ==
- Not Equal To !=
- Greater Than >
- Less Than <
- Greater Than or Equal To >=
- Less Than or Equal To <=
Relational Operators in Detail
1. == (Equal To)
It is used to check if two values are the same.
if (a == b){
System.out.println("Both are equal");
}2. != (Not Equal To)
Checks whether two values are different.
if (a != b){
System.out.println("Values are not equal");
}3. > (Greater Than)
Checks if the left value is greater than the right.
if (marks > 50){
System.out.println("Pass");
}4. < (Less Than)
Checks if the left value is smaller than the right.
if (age < 18){
System.out.println("Minor");
}5. >= (Greater Than or Equal To)
True when the left value is greater than or equal to the right value.
if (height >= 120){
System.out.println("Allowed for ride");
}6. <= (Less Than or Equal To)
True when the left value is smaller than or equal to the right value.
if (temp <= 0){
System.out.println("Freezing point");
}Precedence and Associativity of Relational Operators
When multiple operators appear in a single expression, operator precedence decides which one executes first. In Java, relational operators have lower precedence than arithmetic operators but higher precedence than logical operators.
1. Precedence Order (High → Low):
- > , < , >= , <=
- == , !=
2. Associativity:
All relational operators in Java have left-to-right associativity. Example:
int a = 10, b = 20, c = 30;
System.out.println(a < b && b < c); // trueFinal Thought
Relational operators are like the decision-makers in a Java program. They compare values, help the program choose what to do next, and make Java applications smarter and more interactive. Without them, coding logic would be incomplete!
If you’re thinking of learning programming, Java is a great place to start!


Leave a Comment