Sub topic 10
Floating-Point, Array, and Nullable Fields
This chapter assumes you can write and compile Java classes, understand primitive values and object references, and use List, Set, and Map. It also assumes you know that equals and hashCode must describe the same value.
A value class often contains more than simple integers or strings. Floating-point numbers, arrays, and nullable fields each need careful handling. Using == or the default hashCode for these fields can make two logically equal objects behave differently.
Nullable fields
Calling field.equals(other.field) fails when field is null. Use Objects.equals instead:
- Two
nullreferences are equal. - A
nullreference and a non-null reference are different. - Two non-null references are compared with their
equalsmethods.
Objects.hash is useful for ordinary fields, including nullable fields. It accepts null without throwing an exception.
Floating-point fields
For double and float fields, use Double.compare or Float.compare in equals. These methods define behavior for special values such as NaN, positive zero, and negative zero.
| Comparison | Result with `Double.compare` |
|---|---|
| `NaN` and `NaN` | equal |
| `+0.0` and `-0.0` | different |
| Two ordinary equal values | equal |
The hash code must use compatible floating-point rules. Objects.hash boxes a double as a Double, whose hashCode follows the same distinctions.
Arrays are values, not references
An array's inherited equals and hashCode methods compare its identity. Two separate arrays with the same elements are therefore not equal by default.
Use the matching methods from Arrays:
Arrays.equalscompares one-dimensional array contents.Arrays.hashCodehashes one-dimensional array contents.Arrays.deepEqualsandArrays.deepHashCodehandle nested object arrays.Arrays.toStringcreates readable text for an array.
These methods also handle a null array safely when both sides use the corresponding Arrays method.
A complete implementation
The following class treats label and values as nullable fields. Its constructor clones the array so callers cannot change the object's state through their original array reference.
import java.util.Arrays;
import java.util.Objects;
public final class Sample {
private final String label;
private final double score;
private final int[] values;
public Sample(String label, double score, int[] values) {
this.label = label;
this.score = score;
this.values = values == null ? null : values.clone();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Sample)) {
return false;
}
Sample other = (Sample) o;
return Objects.equals(label, other.label)
&& Double.compare(score, other.score) == 0
&& Arrays.equals(values, other.values);
}
@Override
public int hashCode() {
int result = Objects.hash(label, score);
return 31 * result + Arrays.hashCode(values);
}
@Override
public String toString() {
return "Sample{"
+ "label='" + label + '\''
+ ", score=" + score
+ ", values=" + Arrays.toString(values)
+ '}';
}
public static void main(String[] args) {
Sample first = new Sample("test", Double.NaN, new int[] {1, 2});
Sample second = new Sample("test", Double.NaN, new int[] {1, 2});
System.out.println(first.equals(second)); // true
System.out.println(first.hashCode() == second.hashCode()); // true
System.out.println(first);
}
}The array is cloned on input, but the implementation does not expose a mutable reference through a getter. These two choices help preserve stable equality and hash codes.
Exercise
Which methods should compare a nullable String, a double, and an int[] inside equals?
Show answer
Use Objects.equals for the String, Double.compare for the double, and Arrays.equals for the int[].