Float
and Double
, a caching mechanism is implemented. Certain values are created during the class initialization stage and are reused only being created not using the new
operator (for example by calling the valueOf
method).The cached values are both possible
Boolean
, all Character
values up to '\u007f'
(127
), and all integer numbers from -128
to 127
inclusive. Since Java 7, the upper limit for Integer
can be increased with the parameter java.lang.Integer.IntegerCache.high
.Values are also cached in many other built-in classes like
BigDecimal
, Currency
, and empty collections. Details can be learned from the source code and documentation, as these caches are implemented at the class code level, not by the JVM itself.This particular example has another catch: a wrapper class object is being compared with a primitive. This leads to unboxing and comparison of values. And, despite the fact that 128 is out of the default caching range, the answer to the question is – yes.