Comparisons
Comparison operators produce a boolean result.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
Compare a score
public class Main {
public static void main(String[] args) {
int score = 78;
int passingScore = 70;
boolean passed = score >= passingScore;
System.out.println("Passed: " + passed);
}
}
Output:
Passed: true
The >= operator checks whether score is greater than or equal to passingScore. The result is stored in a boolean variable.
Later you will compare strings with equals. Using == on objects asks a different question about identity.
Compare two numbers in the exercise.