1. Relational Operators¶
1.1. Objectives¶
- Identify and use the six relational operators in Java
1.2. Time Goal¶
- 10 minutes on this section
1.3. Key Terms¶
- boolean
- A data type with only two possible values, true and false.
- relational operator (boolean expression)
- An operator that compares two values and produces a boolean indicating the relationship between them.
1.4. Exercises¶
Note
Java has six relational operators that test the relationship between two values (e.g., whether they are equal, or whether one is greater than the other). The following expressions show how they are used:
x == y // x is equal to y
x != y // x is not equal to y
x > y // x is greater than y
x < y // x is less than y
x >= y // x is greater than or equal to y
x <= y // x is less than or equal to y
The result of a relational operator is one of two special values: true
or false
. These values belong to the data type boolean
, named after the mathematician George Boole. He developed an algebraic way of representing logic.
You are probably familiar with these operators, but notice how Java is different from mathematical symbols like =, ≠, and ≥.
Note
A common error is to use a single =
instead of a double ==
when comparing values.
Remember that =
is the assignment operator, and == is a relational operator. In addition, the operators =< and => do not exist.
The two sides of a relational operator have to be compatible. For example, the expression 5 < “6” is invalid because 5 is an int and “6” is a String. When comparing values of different numeric types, Java applies the same conversion rules we saw previously with the assignment operator. For example, when evaluating the expression 5 < 6.0, Java automatically converts the 5 to 5.0
true
- Incorrect
false
- Correct
Q-50: What is the value of the boolean expression x < 0
after all the statements below have executed
int x = 0;
true
- Correct
false
- Incorrect
Q-51: What is the value of the boolean expression x >= 0
after all the statements below have executed
int x = 1;
true
- Incorrect
false
- Correct
Q-52: What is the value of the boolean expression x == z
after all the statements below have executed
int x = 0;
int y = 1;
int z = y + 1;
true
- Incorrect
false
- Correct
Q-53: What is the value of the boolean expression x != a
after all the statements below have executed
int x = 0;
int a = 10;
x = 8 + 2 ;
true
- Correct
false
- Incorrect
x
is not aboolean
expression- Incorrect, this is a boolean expression, as the variable
x
can further evaluate to a boolean value (either true or false)
Q-54: What is the value of the boolean expression x
after all the statements below have executed
boolean x = true;
true
- Incorrect
false
- Incorrect
- Error
- Correct, the expression is evaluated from left to right, so first
a > b
evaluates totrue
, but the next comparison is thentrue > c
. Because they are incompatible types the comparison results in the following errorerror: bad operand types for binary operator '>' System.out.println(a > b > c); ^ first type: boolean second type: int 1 error
Q-55: What is the value of the boolean expression c > b > a
after all the statements below have executed
int a = 1;
int b = 2;
int c = 3;
true
- Incorrect
false
- Incorrect, these are different types and therefore cannot be compared
- Error
- Correct, these are incompatible types
error: bad operand types for binary operator '<' System.out.println(x < b); ^ first type: int second type: String
Q-56: What is the value of the boolean expression x < b
after all the statements below have executed
int x = 0;
String b = "0";