Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java wrappers and boxing: nulls, identity, and allocation

Last updated: 28 Sept 20263 min read
tutorial
IntermediateBy AITrove Editorial

Boxing converts a primitive value to its wrapper reference; unboxing extracts a primitive from a wrapper and fails when that reference is null.

Java 8+. Use a JDK that supports this release.

Choose a missing-value contract

A sensor reading may be absent. Integer can represent that absence with null; int cannot. Once the application chooses a default, convert at a single boundary so every arithmetic expression does not need another null check.

The program counts only present readings and computes a long total. It does not substitute zero for missing values because that would change the average. If every reading is absent, it reports that fact rather than dividing by zero.

Wrapper identity is irrelevant to numeric equality. Comparing two Integer references with == tests whether the references match; cached small values can make an incorrect test appear correct. Use value equality or unbox after checking absence.

A generic list stores references

List<Integer> cannot store primitive int elements directly. Boxing fits values into the generic reference-type contract, with potential object and indirection costs. An int array is a better representation when a large fixed numeric batch does not require nulls or list operations.

Repeated boxing in a hot loop should be measured with a benchmark that prevents unused results from disappearing. Timing a few iterations in main includes startup effects and does not establish the cost of one boxed operation.

Working program

Java
public class SensorAverage {
    public static void main(String[] args) {
        Integer[] readings = {120, null, 180};
        long total = 0;
        int present = 0;
        for (Integer reading : readings) {
            if (reading != null) {
                total += reading;
                present++;
            }
        }
        if (present == 0) {
            System.out.println("No readings");
        } else {
            System.out.println("mean=" + total / present);
        }
        System.out.println("present=" + present);
    }
}

Output

Output
mean=150
present=2

Cost and design choices

The scan requires O(n) time and O(1) additional storage beyond the input array. The average uses integer division, so it truncates. A decimal average needs an explicit precision and rounding policy, rather than an accidental conversion after division.

Unboxing does not make an update atomic. Incrementing an Integer field still reads, computes, and writes. Study visibility and atomicity before sharing such counters.

Common Mistakes

  • Do not unbox null.
  • Do not compare wrapper references to establish value equality.
  • Do not count absent readings as zero without defining that policy.

Connect the contracts

Compare the boundary explained in Numeric ranges with the assumptions made by this program.

Compare the boundary explained in Representing absence with the assumptions made by this program.

java
boxing
Storage details