Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The values of boxed primitives cannot be directly compared using the == and != operators because they compare object references, not object values. Programmers might could find this behavior surprising because autoboxing memoizes ( or caches) , the values of some primitive variables. Consequently, reference comparisons and value comparisons produce identical results for the subset of values that are memoized.

Autoboxing automatically wraps a value of a primitive type with the corresponding wrapper object. The Java Language Specification Section 5.1.7 "Boxing Conversion," of the Java Language Specification explains which primitive values are memoized during autoboxing:

...