<p>I'm looking for a clear, concise and accurate answer.  </p>

<p>Ideally as the actual answer, although links to good explanations welcome.</p>

[{'answer_id': 13056, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 6, 'selected': False, 'text': '<p>Boxing &amp; unboxing is the process of converting a primitive value into an object oriented wrapper class (boxing), or converting a value from an object oriented wrapper class back to the primitive value (unboxing).</p>\n\n<p>For example, in java, you may need to convert an <code>int</code> value into an <code>Integer</code> (boxing) if you want to store it in a <code>Collection</code> because primitives can\'t be stored in a <code>Collection</code>, only objects.  But when you want to get it back out of the <code>Collection</code> you may want to get the value as an <code>int</code> and not an <code>Integer</code> so you would unbox it.</p>\n\n<p>Boxing and unboxing is not inherently <em>bad</em>, but it is a tradeoff.  Depending on the language implementation, it can be slower and more memory intensive than just using primitives.  However, it may also allow you to use higher level data structures and achieve greater flexibility in your code.</p>\n\n<p>These days, it is most commonly discussed in the context of Java\'s (and other language\'s) "autoboxing/autounboxing" feature.  Here is a <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html" rel="noreferrer">java centric explanation of autoboxing</a>.</p>\n'}, {'answer_id': 13057, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 5, 'selected': False, 'text': "<p>In .Net:</p>\n\n<p>Often you can't rely on what the type of variable a function will consume, so you need to use an object variable which extends from the lowest common denominator - in .Net this is <code>object</code>.</p>\n\n<p>However <code>object</code> is a class and stores its contents as a reference.</p>\n\n<pre><code>List&lt;int&gt; notBoxed = new List&lt;int&gt; { 1, 2, 3 };\nint i = notBoxed[1]; // this is the actual value\n\nList&lt;object&gt; boxed = new List&lt;object&gt; { 1, 2, 3 };\nint j = (int) boxed[1]; // this is an object that can be 'unboxed' to an int\n</code></pre>\n\n<p>While both these hold the same information the second list is larger and slower. Each value in the second list is actually a reference to an <code>object</code> that holds the <code>int</code>.</p>\n\n<p>This is called boxed because the <code>int</code> is wrapped by the <code>object</code>. When its cast back the <code>int</code> is unboxed - converted back to it's value.</p>\n\n<p>For value types (i.e. all <code>structs</code>) this is slow, and potentially uses a lot more space. </p>\n\n<p>For reference types (i.e. all <code>classes</code>) this is far less of a problem, as they are stored as a reference anyway.</p>\n\n<p>A further problem with a boxed value type is that it's not obvious that you're dealing with the box, rather than the value. When you compare two <code>structs</code> then you're comparing values, but when you compare two <code>classes</code> then (by default) you're comparing the reference - i.e. are these the same instance?</p>\n\n<p>This can be confusing when dealing with boxed value types:</p>\n\n<pre><code>int a = 7;\nint b = 7;\n\nif(a == b) // Evaluates to true, because a and b have the same value\n\nobject c = (object) 7;\nobject d = (object) 7;\n\nif(c == d) // Evaluates to false, because c and d are different instances\n</code></pre>\n\n<p>It's easy to work around:</p>\n\n<pre><code>if(c.Equals(d)) // Evaluates to true because it calls the underlying int's equals\n\nif(((int) c) == ((int) d)) // Evaluates to true once the values are cast\n</code></pre>\n\n<p>However it is another thing to be careful of when dealing with boxed values.</p>\n"}, {'answer_id': 13059, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 7, 'selected': False, 'text': '<p>from <a href="http://books.google.com/books?id=_Y0rWd-Q2xkC&amp;pg=PA81&amp;vq=boxing&amp;source=gbs_search_r&amp;cad=1_1&amp;sig=ACfU3U0SIw4v8UZzMH3f5xImIyHZHD90sA#PPA82,M1" rel="noreferrer">C# 3.0 In a Nutshell</a>:</p>\n\n<blockquote>\n  <p>Boxing is the act of casting a value\n  type into a reference type:</p>\n</blockquote>\n\n<pre><code>int x = 9; \nobject o = x; // boxing the int\n</code></pre>\n\n<blockquote>\n  <p>unboxing is... the reverse:</p>\n</blockquote>\n\n<pre><code>// unboxing o\nobject o = 9; \nint x = (int)o; \n</code></pre>\n'}, {'answer_id': 13073, 'author': 'Jonathan Webb', 'author_id': 1518, 'author_profile': 'https://Stackoverflow.com/users/1518', 'pm_score': 2, 'selected': False, 'text': '<p>The .NET FCL generic collections:</p>\n\n<pre><code>List&lt;T&gt;\nDictionary&lt;TKey, UValue&gt;\nSortedDictionary&lt;TKey, UValue&gt;\nStack&lt;T&gt;\nQueue&lt;T&gt;\nLinkedList&lt;T&gt;\n</code></pre>\n\n<p>were all designed to overcome the performance issues of boxing and unboxing in previous collection implementations.</p>\n\n<p>For more, see chapter 16, <a href="http://books.google.com/books?id=C-V_AAAACAAJ&amp;dq=clr+via+c%23&amp;ei=tyKnSPqSI5T2iQHzwJTECQ" rel="nofollow noreferrer"><em>CLR via C# (2nd Edition)</em></a>.</p>\n'}, {'answer_id': 25324, 'author': 'Peter Burns', 'author_id': 101, 'author_profile': 'https://Stackoverflow.com/users/101', 'pm_score': 9, 'selected': True, 'text': '<p>Boxed values are <a href="http://en.wikipedia.org/wiki/Data_structure" rel="noreferrer">data structures</a> that are minimal wrappers around <a href="http://en.wikipedia.org/wiki/Primitive_type" rel="noreferrer">primitive types</a>*.  Boxed values are typically stored as pointers to objects on <a href="http://en.wikipedia.org/wiki/Dynamic_memory_allocation" rel="noreferrer">the heap</a>.</p>\n\n<p>Thus, boxed values use more memory and take at minimum two memory lookups to access: once to get the pointer, and another to follow that pointer to the primitive.  Obviously this isn\'t the kind of thing you want in your inner loops.  On the other hand, boxed values typically play better with other types in the system.  Since they are first-class data structures in the language, they have the expected metadata and structure that other data structures have.</p>\n\n<p>In Java and Haskell generic collections can\'t contain unboxed values.  Generic collections in .NET can hold unboxed values with no penalties.  Where Java\'s generics are only used for compile-time type checking, .NET will <a href="http://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx" rel="noreferrer">generate specific classes for each generic type instantiated at run time</a>.</p>\n\n<p>Java and Haskell have unboxed arrays, but they\'re distinctly less convenient than the other collections.  However, when peak performance is needed it\'s worth a little inconvenience to avoid the overhead of boxing and unboxing.</p>\n\n<p>* For this discussion, a primitive value is any that can be stored on <a href="http://en.wikipedia.org/wiki/Call_stack" rel="noreferrer">the call stack</a>, rather than stored as a pointer to a value on the heap.  Frequently that\'s just the machine types (ints, floats, etc), structs, and sometimes static sized arrays.  .NET-land calls them value types (as opposed to reference types).  Java folks call them primitive types.  Haskellions just call them unboxed.</p>\n\n<p>** I\'m also focusing on Java, Haskell, and C# in this answer, because that\'s what I know.  For what it\'s worth, Python, Ruby, and Javascript all have exclusively boxed values.  This is also known as the "Everything is an object" approach***. </p>\n\n<p>*** Caveat: A sufficiently advanced compiler / JIT can in some cases actually detect that a value which is semantically boxed when looking at the source, can safely be an unboxed value at runtime. In essence, thanks to brilliant language implementors your boxes are sometimes free.</p>\n'}, {'answer_id': 101638, 'author': 'PEELY', 'author_id': 17641, 'author_profile': 'https://Stackoverflow.com/users/17641', 'pm_score': -1, 'selected': False, 'text': '<p>Like anything else, autoboxing can be problematic if not used carefully. The classic is to end up with a NullPointerException and not be able to track it down. Even with a debugger. Try this:</p>\n\n<pre><code>public class TestAutoboxNPE\n{\n    public static void main(String[] args)\n    {\n        Integer i = null;\n\n        // .. do some other stuff and forget to initialise i\n\n        i = addOne(i);           // Whoa! NPE!\n    }\n\n    public static int addOne(int i)\n    {\n        return i + 1;\n    }\n}\n</code></pre>\n'}, {'answer_id': 21588698, 'author': 'vani', 'author_id': 2900075, 'author_profile': 'https://Stackoverflow.com/users/2900075', 'pm_score': 2, 'selected': False, 'text': '<p><code>Boxing</code> is the process of conversion of a value type into a reference type.                                                                      Whereas <code>Unboxing</code> is the conversion of a reference type into a value type.</p>\n\n<pre><code>EX: int i = 123;\n    object o = i;// Boxing\n    int j = (int)o;// UnBoxing\n</code></pre>\n\n<p>Value Types are: <code>int</code>, <code>char</code> and <code>structures</code>, <code>enumerations</code>.\nReference Types are:\n<code>Classes</code>,<code>interfaces</code>,<code>arrays</code>,<code>strings</code> and <code>objects</code></p>\n'}, {'answer_id': 37943012, 'author': 'Sanjay Kumar', 'author_id': 6493838, 'author_profile': 'https://Stackoverflow.com/users/6493838', 'pm_score': 1, 'selected': False, 'text': '<p>Boxing and unboxing facilitates value types to be treated as objects. Boxing means converting a value to an instance of the object reference type. For example, <code>Int</code> is a class and <code>int</code> is a data type. Converting <code>int</code> to <code>Int</code> is an exemplification of boxing, whereas converting <code>Int</code> to <code>int</code> is unboxing. The concept helps in garbage collection, Unboxing, on the other hand, converts object type to value type.</p>\n\n<pre><code>int i=123;\nobject o=(object)i; //Boxing\n\no=123;\ni=(int)o; //Unboxing.\n</code></pre>\n'}, {'answer_id': 69991369, 'author': 'FrankHB', 'author_id': 2307646, 'author_profile': 'https://Stackoverflow.com/users/2307646', 'pm_score': 2, 'selected': False, 'text': '<p>The language-agnostic meaning of a box is just &quot;an object contains some other value&quot;.</p>\n<p>Literally, <em>boxing</em> is an operation to put some value into the box. More specifically, it is an operation to <em>create</em> a new box containing the value. After boxing, the boxed value can be accessed from the box object, by <em>unboxing</em>.</p>\n<p>Note that objects (not OOP-specific) in many programming languages are about <em>identities</em>, but values are not. Two objects are same iff. they have identities not distinguishable in the program semantics. Values can also be the same (usually under some equality operators), but we do not distinguish them as &quot;one&quot; or &quot;two&quot; <em>unique</em> values.</p>\n<p>Providing boxes is mainly about the effort to distinguish side effects (typically, mutation) from the states on the objects otherwise probably invisible to the users.</p>\n<p>A language may limit the allowed ways to access an object and hide the identity of the object by default. For example, typical Lisp dialects has no explicit distinctions between objects and values. As a result, the implementation has the freedom to share the underlying storage of the objects until some mutation operations occurs on the object (so the object must be &quot;detached&quot; after the operation from the shared instance to make the effect visible, i.e. the mutated value stored in the object could be different than the other objects having the old value). This technique is sometimes called <em>object interning</em>.</p>\n<p>Interning makes the program more memory efficient at runtime if the objects are shared without frequent needs of mutation, at the cost that:</p>\n<ul>\n<li>The users cannot distinguish the identity of the objects.\n<ul>\n<li>There are no way to identify an object and to ensure it has states explicitly independent to other objects <em>in the program</em> before some side effects have actually occur (and the implementation does not aggressively to do the interning concurrently; this should be the rare case, though).</li>\n</ul>\n</li>\n<li>There may be more problems on interoperations which require to identify different objects for different operations.</li>\n<li>There are risks that such assumptions can be false, so the performance is actually made worse by applying the interning.\n<ul>\n<li>This depends on the programming paradigm. Imperative programming which mutates objects frequently certainly would not work well with interning.</li>\n</ul>\n</li>\n<li>Implementations depending on COW (copy-on-write) to ensure interning can incur serious performance degradation in concurrent environments.\n<ul>\n<li>Even local sharing specifically for a few internal data structures can be bad. For example, ISO C++ 11 did not allow sharing of the internal elements of <code>std::basic_string</code> for this reason exactly, even at the cost of breaking the ABI on at least one mainstream implementation (libstdc++).</li>\n</ul>\n</li>\n<li>Boxing and unboxing incur performance penalties. This is obvious especially when these operations can be naively avoided by hand but actually not easy for the optimizer. The concrete measurement of the cost depends (on per-implementation or even per-program basis), though.</li>\n</ul>\n<p>Mutable cells, i.e. boxes, are well-established facilities exactly to resolve the problems of the 1st and 2nd bullets listed above. Additionally, there can be immutable boxes for implementation of assignment in a functional language. See <a href="https://srfi.schemers.org/srfi-111/srfi-111.html" rel="nofollow noreferrer">SRFI-111</a> for a practical instance.</p>\n<p>Using mutable cells as function arguments with call-by-value strategy implements the visible effects of mutation being shared between the caller and the callee. The object contained by an box is effectively &quot;called by shared&quot; in this sense.</p>\n<p>Sometimes, the boxes are referred as references (which is technically false), so the shared semantics are named &quot;reference semantics&quot;. This is not correct, because not all references can propagate the visible side effects (e.g. immutable references). References are more about exposing the access <em>by indirection</em>, while boxes are the efforts to expose <em>minimal</em> details of the accesses like whether indirection or not (which is uninterested and better avoided by the implementation).</p>\n<p>Moreover, &quot;value semantic&quot; is irrelevant here. Values are not against to references, nor to boxes. All the discussions above are based on call-by-value strategy. For others (like call-by-name or call-by-need), no boxes are needed to shared object contents in this way.</p>\n<p>Java is probably the first programming language to make these features popular in the industry. Unfortunately, there seem many bad consequences concerned in this topic:</p>\n<ul>\n<li>The overall programming paradigm does not fit the design.</li>\n<li>Practically, the interning are limited to specific objects like immutable strings, and the cost of (auto-)boxing and unboxing are often blamed.</li>\n<li>Fundamental PL knowledge like the definition of the term &quot;object&quot; (as &quot;instance of a class&quot;) in the language specification, as well as the descriptions of parameter passing, are biased compared to the the <a href="https://classes.cs.uoregon.edu/14S/cis607pl/Papers/fundamental-1967.pdf" rel="nofollow noreferrer">original</a>, <a href="https://en.wikipedia.org/wiki/Fundamental_Concepts_in_Programming_Languages" rel="nofollow noreferrer">well-known</a> meaning, during the adoption of Java by programmers.\n<ul>\n<li>At least CLR languages are following the similar parlance.</li>\n</ul>\n</li>\n</ul>\n<p>Some more tips on implementations (and comments to <a href="https://stackoverflow.com/a/25324/2307646">this answer</a>):</p>\n<ul>\n<li>Whether to put the objects on the call stacks or the heap is an implementation details, and irrelevant to the implementation of boxes.\n<ul>\n<li>Some language implementations do not maintain a contiguous storage as the call stack.</li>\n<li>Some language implementations do not even make the (per thread) activation records a linear stack.</li>\n<li>Some language implementations do allocate stacks on the free store (&quot;the heap&quot;) and transfer slices of frames between the stacks and the heap back and forth.</li>\n<li>These strategies has nothing to do boxes. For instance, many Scheme implementations have boxes, with different activation records layouts, including all the ways listed above.</li>\n</ul>\n</li>\n<li>Besides the technical inaccuracy, the statement &quot;everything is an object&quot; is irrelevant to boxing.\n<ul>\n<li>Python, Ruby, and JavaScript all use latent typing (by default), so all identifiers referring to some objects will evaluate to values having the same static type. So does Scheme.</li>\n<li>Some JavaScript and Ruby implementations use the so-called <a href="https://softwareengineering.stackexchange.com/questions/185406/what-is-the-purpose-of-nan-boxing">NaN-boxing</a> to allow inlining allocation of some objects. Some others (including <a href="https://softwareengineering.stackexchange.com/a/185431/154697">CPython</a>) do not. With NaN boxing, a normal <code>double</code> object needs no unboxing to access its value, while a value of some other types can be boxed in a host <code>double</code> object, and there is no reference for <code>double</code> or the boxed value. With the naive pointer approach, a value of host object pointer like <code>PyObject*</code> is an object reference holding a box whose boxed value is stored in the dynamically allocated space.</li>\n<li><a href="https://stackoverflow.com/questions/865911/is-everything-an-object-in-python-like-ruby">At least in Python</a>, objects are not &quot;everything&quot;. They are also not known as &quot;boxed values&quot; unless you are talking about interoperability with specific implementations.</li>\n</ul>\n</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/13055', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/905/']