Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:
-
From type
booleanto typeBoolean -
From type
byteto typeByte -
From type
shortto typeShort -
From type
charto typeCharacter -
From type
intto typeInteger -
From type
longto typeLong -
From type
floatto typeFloat -
From type
doubleto typeDouble -
From the null type to the null type
This rule is necessary because the conditional operator (§15.25) applies boxing conversion to the types of its operands, and uses the result in further calculations.
At run time, boxing conversion proceeds as follows:
-
If
pis a value of typeboolean, then boxing conversion convertspinto a referencerof class and typeBoolean, such thatr.booleanValue() == p -
If
pis a value of typebyte, then boxing conversion convertspinto a referencerof class and typeByte, such thatr.byteValue() == p -
If
pis a value of typechar, then boxing conversion convertspinto a referencerof class and typeCharacter, such thatr.charValue() == p -
If
pis a value of typeshort, then boxing conversion convertspinto a referencerof class and typeShort, such thatr.shortValue() == p -
If
pis a value of typeint, then boxing conversion convertspinto a referencerof class and typeInteger, such thatr.intValue() == p -
If
pis a value of typelong, then boxing conversion convertspinto a referencerof class and typeLong, such thatr.longValue() == p -
If
pis a value of typefloatthen:- If
pis not NaN, then boxing conversion convertspinto a referencerof class and typeFloat, such thatr.floatValue()evaluates top - Otherwise, boxing conversion converts
pinto a referencerof class and typeFloatsuch thatr.isNaN()evaluates totrue
- If
-
If
pis a value of typedouble, then:- If
pis not NaN, boxing conversion convertspinto a referencerof class and typeDouble, such thatr.doubleValue()evaluates top - Otherwise, boxing conversion converts
pinto a referencerof class and typeDoublesuch thatr.isNaN()evaluates totrue
- If
-
If
pis a value of any other type, boxing conversion is equivalent to an identity conversion (§5.1.1).
If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part. This allows (but does not require) sharing of some or all of these references. Notice that integer literals of type long are allowed, but not required, to be shared.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.
A boxing conversion may result in an OutOfMemoryError if a new instance of one of the wrapper classes (Boolean, Byte, Character, Short, Integer, Long, Float, or Double) needs to be allocated and insufficient storage is available.