CKTIS / Java
Comparing Integer objects: values, references, and null
Distinguish object identity from value equality, see when `==` unboxes an `Integer` to an `int`, and compare boxed values safely when either reference may be `null`.
Version: Java SE 21
Syntax
Instance methods on java.lang.Integer: boolean equals(Object obj); int intValue(). Static method on java.util.Objects: static boolean equals(Object a, Object b). With Integer a and b and int n: a == b compares references; a == n unboxes a and compares values.When both operands have type Integer, == compares references. It returns true if they refer to the same object or are both null. Two distinct objects can hold the same int. Integer.valueOf(int) caches values from −128 through 127 inclusive and may cache other values too. Do not use == to compare the values of two Integer objects.
The Integer.equals/intValue distinction is about comparing a boxed value versus extracting a primitive. The instance method Integer.equals(Object) returns true only when the argument is an Integer holding the same int. A null argument or a Long with the same numeric value gives false. Calling the method on a null receiver throws NullPointerException.
Integer.intValue() returns the primitive int. When == compares an Integer with an int, Java automatically unboxes the Integer and compares numeric values. Both calling intValue() on a null reference and automatically unboxing null throw NullPointerException.
The static method java.util.Objects.equals(Object, Object) returns true for two null arguments. For two Integer arguments, it returns false if exactly one is null; if neither is null, it follows Integer value equality. Use Objects.equals when either reference may be null.
Examples
import java.util.Objects;
public class IntegerComparison {
@SuppressWarnings("removal") // Fresh objects for this demonstration only.
public static void main(String[] args) {
Integer a = new Integer(42);
Integer b = new Integer(42);
Integer missing = null;
System.out.println("same reference: " + (a == b));
System.out.println("Integer.equals: " + a.equals(b));
System.out.println("Integer vs Long: " + a.equals(Long.valueOf(42L)));
System.out.println("intValue comparison: " + (a.intValue() == b.intValue()));
System.out.println("Integer vs int: " + (a == 42));
System.out.println("Objects.equals, same value: " + Objects.equals(a, b));
System.out.println("Objects.equals, one null: " + Objects.equals(a, missing));
System.out.println("Objects.equals, both null: " + Objects.equals(missing, null));
try {
System.out.println(missing == 42);
} catch (NullPointerException e) {
System.out.println("null unboxing: NullPointerException");
}
}
}Result
same reference: false Integer.equals: true Integer vs Long: false intValue comparison: true Integer vs int: true Objects.equals, same value: true Objects.equals, one null: false Objects.equals, both null: true null unboxing: NullPointerException
The two `new Integer(42)` calls deliberately create distinct objects with the same value, regardless of caching. The constructor is deprecated for removal and is used only for this demonstration. In ordinary code, use autoboxing or `Integer.valueOf(int)`. The final `catch` block shows what happens when `null` is unboxed.
Related lessons
- Java · Lesson 48 · Advanced · Version: 2