Contents
Page-10
Prev
Next
Page+10
Index
Boxed Number
A boxed number, such as this instance of Integer, is nice
because it can be treated as an object, used in structures such as
LinkedList<Integer>, and has useful methods.
However:
- Integer has the same numeric accuracy as an int,
32 bits.
- Integer requires 16 bytes of storage, while int
only requires 4 bytes.
- Integer is immutable:
since it is possible that multiple variables point to it, we cannot
change its value. Double, String, etc. are immutable.
- Doing arithmetic (even ++) may require creation of a new box
(allocation of new storage), often making the old box garbage.
(Java caches small Integer values to make them
more efficient.)
- Boxes are somewhat expensive because they must be
garbage collected. The garbage collector is a highly paid worker.