Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Boxing (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Boxing== Boxing's most prominent use is in [[Java (programming language)|Java]] where there is a distinction between [[reference type|reference]] and [[value type]]s for reasons such as runtime efficiency and syntax and semantic issues. In Java, a {{Java|LinkedList}} can only store values of type {{Java|Object}}. One might desire to have a {{Java|LinkedList}} of {{Java|int}}, but this is not directly possible. Instead Java defines [[primitive wrapper class]]es corresponding to each [[primitive data type|primitive type]]: {{Java|Integer}} and {{Java|int}}, {{Java|Character}} and {{Java|char}}, {{Java|Float}} and {{Java|float}}, etc. One can then define a {{Java|LinkedList}} using the boxed type {{Java|Integer}} and insert {{Java|int}} values into the list by boxing them as {{Java|Integer}} objects. (Using [[generic programming|generic]] parameterized types introduced in [[Java Platform, Standard Edition|J2SE]] 5.0, this type is represented as {{Java|LinkedList<Integer>}}.) On the other hand, [[C Sharp (programming language)|C#]] has no primitive wrapper classes, but allows boxing of any value type, returning a generic {{C sharp|Object}} reference. In [[Objective-C]], any primitive value can be prefixed by a {{ObjC|@}} to make an {{ObjC|NSNumber}} out of it (e.g. {{ObjC|@123}} or {{ObjC|@(123)}}). This allows for adding them in any of the standard collections, such as an {{ObjC|NSArray}}. [[Haskell]] has little or no notion of [[reference type]], but still uses the term "boxed" for the runtime system's uniform pointer-to-[[tagged union]] representation.<ref>{{cite web |title=7.2. Unboxed types and primitive operations |url=https://downloads.haskell.org/~ghc/6.12.1/docs/html/users_guide/primitives.html |website=downloads.haskell.org |access-date=10 August 2022}}</ref> The boxed object is always a copy of the value object, and is usually [[immutable object|immutable]]. Unboxing the object also returns a copy of the stored value. Repeated boxing and unboxing of objects can have a severe performance impact, because boxing [[dynamic memory allocation|dynamically allocates]] new objects and unboxing (if the boxed value is no longer used) then makes them eligible for [[garbage collection (computer science)|garbage collection]]. However, modern garbage collectors such as the default Java HotSpot garbage collector can more efficiently collect short-lived objects, so if the boxed objects are short-lived, the performance impact may not be severe. In some languages, there is a direct equivalence between an unboxed primitive type and a reference to an immutable, boxed object type. In fact, it is possible to substitute all the primitive types in a program with boxed object types. Whereas assignment from one primitive to another will copy its value, assignment from one reference to a boxed object to another will copy the reference value to refer to the same object as the first reference. However, this will not cause any problems, because the objects are immutable, so there is semantically no real difference between two references to the same object or to different objects (unless you look at physical equality). For all operations other than assignment, such as arithmetic, comparison, and logical operators, one can unbox the boxed type, perform the operation, and re-box the result as needed. Thus, it is possible to not store primitive types at all.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)