Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
A boxing conversion converts the value of a primitive type to the corresponding value of the reference type, for example, from {{int}} to {{Integer}} \[[JLS 2005|AA. Bibliography#JLS 05]\]. This is convenient in cases where an object parameter is required, such as with collection classes like {{Map}} and {{List}}. Another use case is for interoperation with methods that require their parameters to be object references rather than primitive types. Automatic conversion to the resulting wrapper types also reduces clutter in code.

Expressions autobox into the intended type when the reference type causing the boxing conversion is one of the specific numeric wrapper types (e.g., Byte, Short, Integer, Long, Float, or Double). However, autoboxing can produce unexpected results when the reference type causing the boxing conversion is nonspecific (e.g., Number or Object) and the value being converted is the result of an expression that mixes primitive numeric types. In this latter case, the specific wrapper type that results from the boxing conversion is chosen based on the numeric promotion rules governing the expression evaluation. See rule "NUM18-J. Be aware of numeric promotion behavior" for additional explanation of the details of the promotion rules. Consequently, programs that use primitive arithmetic expressions as actual arguments passed to method parameters that have non-specific reference types must cast the expression to the intended primitive numeric type before the boxing conversion takes place.

Noncompliant Code Example

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a92e9eb99beccba8-faec5d3d-46364305-98e0b226-4d39f044845a0a9e470c318c"><ac:plain-text-body><![CDATA[

[[Core Java 2004

AA. Bibliography#Core Java 04]]

Chapter 5

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7e14d1059e9b7714-b93a3768-488b47f2-842bb165-2983f8df9c93578b29978931"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§5.1.7, "Boxing Conversions"

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="87d3484440f928d0-e33b6c31-4a244737-8bd0a097-b402ec0885cd97a539033ccb"><ac:plain-text-body><![CDATA[

[[Techtalk 2007

AA. Bibliography#Techtalk 07]]

"The Joy of Sets"

]]></ac:plain-text-body></ac:structured-macro>

...