The method exists for many types: Integer
, Long
, BigDecimal
and others and the documentation is always the same: Under some circumstances (which aren't defined), the method can return the same result.
AFAIK, the caching is only implemented for integer types and it returns cached instances for values between -128 and 127 (most common values). For BigDecimal
, the cache currently works for values from 0 to 10.
Later versions of Java might extend this behavior to other values/more types. So it's smart to use this code today because it might make your code faster tomorrow (and the code won't be slower today).
The Java compiler, for example, uses this API when generating code for autoboxing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…