Sub topic 7
Comparing Java Wrapper Classes Correctly
This chapter assumes that you know Java primitive types, classes, objects, ==, equals, and generic collections. The focus is how Java compares wrapper objects such as Integer and Double.
The working model
A wrapper class is an object that holds a primitive value. For example, Integer holds an int, and Double holds a double.
When both sides of == are wrapper references, Java compares object identity. It asks whether both references point to the same object. It does not normally compare the numeric values.
The equals method compares values according to the wrapper class's rules. The compareTo method orders two values and returns a negative number, zero, or a positive number.
| Expression | What it usually means for wrappers |
|---|---|
| `a == b` | The two references point to the same object |
| `a.equals(b)` | The two wrapper values are equal |
| `a.compareTo(b) == 0` | The two values compare as equal |
| `Objects.equals(a, b)` | Equal values, including two `null` references |
Why `==` can give surprising results
Java requires Integer.valueOf to reuse objects for values from -128 through 127. This is called the integer cache. Java implementations can cache additional values, but code must not depend on that behavior.
Autoboxing uses Integer.valueOf, so small values can refer to the same cached object. Larger values often create separate objects.
public class WrapperEquality {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 1000;
Integer d = 1000;
System.out.println(a == b); // commonly true: cached object
System.out.println(c == d); // commonly false: different objects
System.out.println(c.equals(d)); // true: equal Integer values
System.out.println(c.compareTo(d) == 0); // true
}
}The important rule is simple: use equals or compareTo for wrapper values. Never use == to test whether two separately obtained wrapper objects contain the same value.
`equals`, `compareTo`, and null
Calling an instance method on null throws NullPointerException:
Integer count = null;
// count.equals(0); // NullPointerExceptionWhen a wrapper may be null, Objects.equals is a safe value comparison. It returns true for two null references, false for one null reference, and otherwise calls equals.
import java.util.Objects;
public class NullableComparison {
public static void main(String[] args) {
Integer expected = null;
Integer actual = null;
System.out.println(Objects.equals(expected, actual)); // true
System.out.println(Objects.equals(actual, 0)); // false
}
}compareTo is useful when sorting or checking order, but both operands must be non-null. It is also safer to check its sign instead of expecting a particular nonzero result:
if (left.compareTo(right) < 0) {
System.out.println("left comes before right");
}Do not write left.compareTo(right) == -1. The contract promises a negative result, not specifically -1.
Special rules for `Double`
Double.equals follows object-value rules that differ from primitive double ==. In particular, a Double containing NaN equals another Double containing NaN. It also treats positive zero and negative zero as different.
public class DoubleEquality {
public static void main(String[] args) {
Double firstNaN = Double.NaN;
Double secondNaN = Double.NaN;
System.out.println(firstNaN.equals(secondNaN)); // true
System.out.println(Double.valueOf(0.0).equals(-0.0)); // false
System.out.println(Double.NaN == Double.NaN); // false
}
}Choose the comparison that matches the requirement. Use primitive == when you need IEEE floating-point comparison. Use Double.equals when wrapper equality and its special-value rules are intended.
A practical decision guide
- Use
==for primitives. - Use
equalsfor non-null wrapper values. - Use
Objects.equalswhen either wrapper may benull. - Use
compareTofor ordering. - Do not depend on wrapper caching.
- Remember that unboxing a
nullwrapper fails before a primitive comparison can occur.
Exercise
Why can Integer a = 127; Integer b = 127; a == b produce a different result from the same expression using 1000, and what comparison should replace it?
Show answer
Small Integer values come from a required cache, so a and b may reference the same object. Larger values may use different objects. Replace == with a.equals(b) for non-null values or Objects.equals(a, b) when either value may be null.