Comparison operators
A condition is usually a comparison. Java has six comparison operators, and each one gives true or false:
| Operator | Means | Example that's true |
|---|---|---|
> |
greater than | 5 > 3 |
>= |
greater than or equal to | 3 >= 3 |
< |
less than | 2 < 7 |
<= |
less than or equal to | 7 <= 7 |
== |
equal to | 4 == 4 |
!= |
not equal to | 4 != 5 |
== compares, = assigns
Comparing uses two equals signs. With one, Java reads it as an assignment. An assignment has a value too, the value it stores: here that's the int 5, and an if needs true or false:
public class Main {
public static void main(String[] args) {
int number = 5;
if (number = 5) {
System.out.println("Five!");
}
}
}
Main.java:4: error: incompatible types: int cannot be converted to boolean
if (number = 5) {
^
1 errorWith numbers the compiler catches this mistake. With a boolean variable (the next module introduces them) it can't: if (done = true) stores true in done, compiles without a warning, and the block always runs. To compare, always use ==.
These operators compare numbers. Comparing text is different, and the next module shows how.