Sub topic 8
Comparing Primitive Values
A comparison asks Java whether a statement about two values is true or false. For example, you might compare a test score with a passing score.
A primitive type is a built-in type for simple values. Examples include int for whole numbers, double for decimal-like numbers, char for single characters, and boolean for true or false.
A comparison is an expression: a piece of code that produces a value. It uses an operator, a symbol that tells Java what to do, with two operands, the values being compared. The result has type boolean.
Comparison operators
Suppose score is an int with the value 70.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | Equal to | `score == 70` | `true` |
| `!=` | Not equal to | `score != 70` | `false` |
| `<` | Less than | `score < 70` | `false` |
| `>` | Greater than | `score > 60` | `true` |
| `<=` | Less than or equal to | `score <= 70` | `true` |
| `>=` | Greater than or equal to | `score >= 70` | `true` |
The difference between > and >= matters at the boundary, the value where the result changes. If 70 is a passing score, use score >= 70. Using score > 70 would leave out a score of exactly 70.
Try comparisons in a program
Save this program as ComparisonDemo.java:
public class ComparisonDemo {
public static void main(String[] args) {
int score = 70;
boolean passed = score >= 70;
System.out.println(passed); // true
System.out.println(score > 70); // false
System.out.println(score != 100); // true
System.out.println(7 == 7.0); // true
System.out.println('A' < 'a'); // true
}
}Run it from the folder containing the file:
javac ComparisonDemo.java
java ComparisonDemoThe first comparison produces true, which the program stores in the passed variable. The second comparison uses the same boundary but does not include equality, so it produces false.
Java can compare different numeric types, such as int and double. It converts them to compatible types before comparing. Java compares char values by their numeric character codes, not by dictionary order. For boolean values, use == or !=; operators such as < do not apply.
Common mistakes
Do not confuse == with =. The operator == compares values. A single = assigns a value to a variable.
Also, Java does not accept a chained comparison such as 40 < score < 90. The first comparison produces a boolean, which cannot then be compared with 90 using <.
A floating-point number is a number stored in a form that can represent fractions. A double is a floating-point type. Some decimal values cannot be stored exactly, so an equality comparison can be surprising:
public class DecimalComparison {
public static void main(String[] args) {
System.out.println(0.1 + 0.2 == 0.3); // false
}
}For measurements, consider comparing the difference against a suitable tolerance, a small allowed difference. For money, one option is to store whole cents instead.
Practice
Exercise
If age is 18, what do age >= 18 and age > 18 produce?
Show answer
age >= 18 produces true. age > 18 produces false because 18 is not greater than itself.
Exercise
Which operator checks whether two primitive values are not equal? What does 5 != 5 produce?
Show answer
Use !=. The expression produces false because the values are equal.