Relational Operators in Java | About, Types and Examples
Have you ever wondered how Java programs make decisions? Relational operators like ==, !=, >, <, >=, and <= are key in Java for comparing values, enabling the code to make decisions and effectively control the flow based on these comparisons. They are essential for implementing logic in conditional statements and loops.Let's understand more!
Table of Content
- What is a Relational Operator in Java?
- Types and Syntax of Relational Operators
- Examples Showing Usage of Relational Operator
Best-suited Java courses for you
Learn Java with these high-rated online courses
What is a Relational Operator in Java?
Relational operators in Java are used to establish a relationship between two operands. They compare the values and return a boolean result, either true or false. These operators are primarily used in conditions that control the flow of the program, such as in if statements, loops (for, while, do-while), and ternary operations. It's important to note that when comparing objects, relational operators compare references, not the content of the objects.
Types and Syntax of Relational Operators
Operator Type |
Symbol |
Syntax Example |
Use Case |
Notes |
Equal to |
== |
a == b |
Checks if two values are equal |
Commonly used in conditional statements |
Not Equal to |
!= |
a != b |
Checks if two values are not equal |
Useful for checking inequality |
Greater than |
> |
a > b |
Checks if the left operand is greater than the right operand |
Determines if one value is larger than another |
Less than |
< |
a < b |
Checks if the left operand is less than the right operand |
Useful for sorting and range checks |
Greater than or equal to |
>= |
a >= b |
Checks if the left operand is greater than or equal to the right operand |
Often used in loops and conditionals |
Less than or equal to |
<= |
a <= b |
Checks if the left operand is less than or equal to the right operand |
Common in conditional logic involving ranges or limits |
Points to be Noted :
- a and b are operands and can be literals, variables, or expressions.
- These operators are commonly used with numeric data types (like int, float, double) but can also be used with characters (char).
- In the case of object references, == and != check for reference equality, not the equality of the content.
- Relational operators are crucial in control flow structures such as if statements, loops, and ternary operators for making decisions based on comparing values.
Examples Showing Usage of Relational Operators
Example 1: Age Verification for Eligibility
Problem Statement: Check if a person is eligible to vote based on their age.
public class AgeVerification { public static void main(String[] args) { int age = 20;
// Check if age is greater than or equal to 18 if (age >= 18) { System.out.println("Eligible to vote."); } else { System.out.println("Not eligible to vote."); } }}
Output
Eligible to vote.
Example 2: Comparing Test Scores
Problem Statement: Determine if a student has scored higher than the passing score.
public class TestScoreComparison { public static void main(String[] args) { int studentScore = 75; int passingScore = 60;
// Check if student score is greater than passing score if (studentScore > passingScore) { System.out.println("Student passed the test."); } else { System.out.println("Student failed the test."); } }}
Output
Student passed the test.
Example 3: Validating User Input Range
Problem Statement: Ensure a number entered by the user falls within a specific range.
public class InputRangeValidation { public static void main(String[] args) { int userInput = 35;
// Check if the input is within the range 1 to 100 if (userInput >= 1 && userInput <= 100) { System.out.println("Valid input."); } else { System.out.println("Invalid input. Please enter a number between 1 and 100."); } }}
Output
Valid input.
Example 4: Checking Temperature for Freezing Point
Problem Statement: Determine if the water is in a solid state at a given temperature.
public class WaterStateCheck { public static void main(String[] args) { int temperature = -5; // Temperature in degrees Celsius
// Check if temperature is less than or equal to 0 if (temperature <= 0) { System.out.println("Water is frozen."); } else { System.out.println("Water is not frozen."); } }}
Output
Water is frozen.
Example 5: Finding the Largest of Two Numbers
Problem Statement: Compare two numbers and find the larger one.
public class LargestNumber { public static void main(String[] args) { int number1 = 45; int number2 = 30;
// Check which number is greater if (number1 > number2) { System.out.println("The larger number is: " + number1); } else { System.out.println("The larger number is: " + number2); } }}
Output
The larger number is: 45
Conclusion
Thus, relational operators play a vital role in Java programming by enabling the comparison of values and making decisions based on these comparisons. They are simple yet powerful tools that are essential for implementing logic in various applications.
FAQs
What are Relational Operators in Java?
Relational operators in Java are used to compare two values and determine the relationship between them. They return a boolean result (true or false) based on the comparison.
What are the Types of Relational Operators in Java?
Java includes six relational operators:
- == (equal to)
- != (not equal to)
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)
How are Relational Operators Used in Java?
Relational operators are used to compare two values or expressions. For example, a == b checks if a is equal to b, and x > y checks if x is greater than y.
What is the Difference Between == and equals() Method in Java?
== is a relational operator used to compare object references, while the equals() method is used to compare the content or value of objects. In most cases, when comparing objects for equality, it's recommended to use the equals() method.
Can Relational Operators Be Used with Non-Primitive Data Types?
Yes, relational operators can be used with non-primitive data types (objects) as long as those objects have implemented the equals() method. The equals() method defines how objects of a class are compared for equality.
Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio