Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A comparison is an expression whose result has type boolean. The operators <, >, <=, >=, ==, and != compare the values of their operands, then produce either true or false. They do not print anything, change either variable, or choose what the program does next by themselves.
int mayaScore = 72;
int passScore = 75;
boolean below = mayaScore < passScore; // true
boolean above = mayaScore > passScore; // false
boolean atLeast = mayaScore >= passScore; // false
boolean atMost = mayaScore <= passScore; // true
boolean equal = mayaScore == passScore; // false
boolean different = mayaScore != passScore; // trueWith mayaScore equal to 72 and passScore equal to 75, the score is below the passing score, so mayaScore < passScore produces true. The same values make mayaScore > passScore, mayaScore >= passScore, and mayaScore == passScore produce false. The expressions mayaScore <= passScore and mayaScore != passScore produce true.
What are the result and type of mayaScore < passScore when mayaScore is 72 and passScore is 75?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A comparison operator reads its left operand first and its right operand second. mayaScore >= passScore asks whether 72 is at least 75, so it produces false. Reversing the variables creates passScore >= mayaScore, which asks whether 75 is at least 72, so it produces true. The operator did not change, but the question did.
boolean mayaReachesPass = mayaScore >= passScore; // false
boolean passReachesMaya = passScore >= mayaScore; // true
boolean mayaIsLower = mayaScore < passScore; // true
boolean passIsLower = passScore < mayaScore; // falseYou can read mayaScore < passScore as "is Maya's score less than the passing score?" Reading passScore < mayaScore asks a different question: "is the passing score less than Maya's score?" Treating comparisons as symmetric hides this difference and can make a correct operator appear to give the wrong answer.
The operator == checks whether two values are equal and produces a boolean result. For the current values, mayaScore == passScore produces false because 72 and 75 are different. A single = is the assignment operator, so mayaScore = passScore does not ask a question. It changes mayaScore from 72 to 75.
int mayaScore = 72;
int passScore = 75;
boolean sameScore = mayaScore == passScore; // false
mayaScore = passScore; // mayaScore is now 75After the assignment, the original score of 72 is gone from mayaScore, and both variables contain 75. That is a state change, not a comparison result. In Java, writing = where you meant == is rejected when the surrounding code requires a boolean expression, but the two operators still have completely different jobs.
Replace the operator in mayaScore = passScore so the expression checks equality without changing mayaScore.
mayaScore = passScoreCheckpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The greater-than operator > excludes equality, while >= includes it. With the current values, mayaScore > passScore asks whether 72 is greater than 75 and produces false. mayaScore >= passScore also produces false because 72 is not greater than 75 and is not equal to it.
Now consider 75 as a hypothetical replacement value for mayaScore while passScore remains 75. Then mayaScore > passScore is false because the values are equal, but mayaScore >= passScore is true because greater than or equal to includes the equality case. The same boundary rule applies in the other direction: <= includes equality, while < excludes it.
| OPERATOR | INCLUDES EQUAL VALUES? | MEANING AT MAYASCORE = 75 AND PASSSCORE = 75 |
|---|---|---|
| > | No | false |
| >= | Yes | true |
| < | No | false |
| <= | Yes | true |
| == | Only equality | true |
| != | Only difference | false |