Unit 3: Boolean Expressions and If Statements
Relational Operators in Java for AP CSA
Introduction to Relational Operators
Relational operators enable you to compare values in Java, forming the basis for many decision-making constructs. They evaluate the relationship between two operands and return a boolean result based on that comparison.
Core Concepts
- Relational Operators: Instruments that assess the relation between two values.
- Boolean Results: Outcomes of the comparisons are always boolean values, i.e., true or false.
Java's Relational Operators
Relational Operators Table
Operator | Name | Description |
---|---|---|
< | Less Than | Checks if the left operand is less than the right operand. |
> | Greater Than | Checks if the left operand is greater than the right operand. |
<= | Less Than Equals | Checks if the left operand is less than or equal to the right. |
>= | Greater Than Equals | Checks if the left operand is greater than or equal to the right. |
== | Equals | Checks if two operands are equal. |
!= | Not Equals | Checks if two operands are not equal. |
Less Than <
- E.g., (3 < 5) results in true but (5 < 3) results in false.
Greater Than >:
- E.g., (5 > 3) results in true but (3 > 5) results in false.
Less Than or Equal To <=:
- E.g., (3 <= 3) results in true.
Greater Than or Equal To >=:
- E.g., (5 >= 3) results in true.
Equals ==:
- E.g., (5 == 5) results in true but (5 == 3) results in false.
Not Equals !=:
- E.g., (5 != 3) results in true.
Usage in Decision Making
Relational operators frequently appear within decision-making constructs like if statements, allowing programs to respond differently based on the provided input or existing conditions.
int age = 15;
if (age >= 18) {
System.out.println("Eligible to vote.");
} else {
System.out.println("Not eligible to vote.");
}
Combining with Logical Operators
Relational operators can be combined with logical operators to craft intricate conditions.
int age = 20;
String nationality = "American";
if ((age >= 18) && (nationality.equals("American"))) {
System.out.println("Eligible to vote in the US.");
}
Common Pitfalls
- Using = instead of ==: A common mistake is using the assignment operator = instead of the equality operator ==. This results in an assignment rather than a comparison.
- Comparing Objects: Using == to compare objects checks for reference equality. To compare the contents of objects, methods like equals() should be used.
Practical Applications
Data Validation
Relational operators can be employed to validate user input, ensuring data falls within acceptable ranges or meets specific criteria.
int temperature = 25;
if (temperature > 0 && temperature < 100) {
System.out.println("Temperature is within safe limits.");
}
Summary
In this guide, we've covered the crucial role of relational operators in Java and how they're used to compare values. These operators are foundational for decision-making constructs in Java, and understanding their functionality is vital for anyone studying AP CSA.
References
- Relational Operators in Java by Oracle
Homework for AP CSA Students
Assignment
Instructions
Create a program that compares two integers input by the user. The program should display if the first number is greater than, less than, or equal to the second number.
Expand the program to compare three numbers and determine the largest and smallest among them.
Challenge: Input a year from the user and determine if it's a leap year using relational operators.