I want to understand purposes of this
optimization. In what cases
performance is increased, etc.
Reference to some research of this
problem will be great.
The purpose is mainly to save memory, which also leads to faster code due to better cache efficiency.
Basically, the Integer
class keeps a cache of Integer
instances in the range of -128 to 127, and all autoboxing, literals and uses of Integer.valueOf()
will return instances from that cache for the range it covers.
This is based on the assumption that these small values occur much more often than other ints and therefore it makes sense to avoid the overhead of having different objects for every instance (an Integer
object takes up something like 12 bytes).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…