fbpx
Wikipedia

Boxing (computer science)

In computer science, boxing (a.k.a. wrapping) is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrapper object. Autoboxing is the term for automatically applying boxing and/or unboxing transformations as needed.

Boxing edit

Boxing's most prominent use is in Java where there is a distinction between reference and value types for reasons such as runtime efficiency and syntax and semantic issues. In Java, a LinkedList can only store values of type Object. One might desire to have a LinkedList of int, but this is not directly possible. Instead Java defines primitive wrapper classes corresponding to each primitive type: Integer and int, Character and char, Float and float, etc. One can then define a LinkedList using the boxed type Integer and insert int values into the list by boxing them as Integer objects. (Using generic parameterized types introduced in J2SE 5.0, this type is represented as LinkedList<Integer>.)

On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference. In Objective-C, any primitive value can be prefixed by a @ to make an NSNumber out of it (e.g. @123 or @(123)). This allows for adding them in any of the standard collections, such as an 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.[1]

The boxed object is always a copy of the value object, and is usually 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 dynamically allocates new objects and unboxing (if the boxed value is no longer used) then makes them eligible for 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.

Autoboxing edit

Autoboxing is the term for getting a reference type out of a value type just through type conversion (either implicit or explicit). The compiler automatically supplies the extra source code that creates the object.

For example, in versions of Java prior to J2SE 5.0, the following code did not compile:

Integer i = new Integer(9); Integer i = 9; // error in versions prior to 5.0! 

Compilers prior to 5.0 would not accept the last line. Integer are reference objects, on the surface no different from List, Object, and so forth. To convert from an int to an Integer, one had to "manually" instantiate the Integer object. As of J2SE 5.0, the compiler will accept the last line, and automatically transform it so that an Integer object is created to store the value 9.[2] This means that, from J2SE 5.0 on, something like Integer c = a + b, where a and b are Integer themselves, will compile now - a and b are unboxed, the integer values summed up, and the result is autoboxed into a new Integer, which is finally stored inside variable c. The equality operators cannot be used this way, because the equality operators are already defined for reference types, for equality of the references; to test for equality of the value in a boxed type, one must still manually unbox them and compare the primitives, or use the Objects.equals method.

Another example: J2SE 5.0 allows the programmer to treat a collection (such as a LinkedList) as if it contained int values instead of Integer objects. This does not contradict what was said above: the collection still only contains references to dynamic objects, and it cannot list primitive types. It cannot be a LinkedList<int>, but it must be a LinkedList<Integer> instead. However, the compiler automatically transforms the code so that the list will "silently" receive objects, while the source code only mentions primitive values. For example, the programmer can now write list.add(3) and think as if the int 3 were added to the list; but, the compiler will have actually transformed the line into list.add(new Integer(3)).

Automatic unboxing edit

With automatic unboxing the compiler automatically supplies the extra source code that retrieves the value out of that object, either by invoking some method on that object, or by other means.

For example, in versions of Java prior to J2SE 5.0, the following code did not compile:

Integer k = new Integer(4); int l = k.intValue(); // always okay int m = k; // would have been an error, but okay now 

C# doesn't support automatic unboxing in the same meaning as Java, because it doesn't have a separate set of primitive types and object types. All types that have both primitive and object version in Java, are automatically implemented by the C# compiler as either primitive (value) types or object (reference) types.

In both languages, automatic boxing does not downcast automatically, i.e. the following code won't compile:

C#:

int i = 42; object o = i; // box int j = o; // unbox (error) Console.WriteLine(j); // unreachable line, author might have expected output "42" 

Java:

int i = 42; Object o = i; // box int j = o; // unbox (error) System.out.println(j); // unreachable line, author might have expected output "42" 

Type helpers edit

Modern Object Pascal has yet another way to perform operations on simple types, close to boxing, called type helpers in FreePascal or record helpers in Delphi and FreePascal in Delphi mode.
The dialects mentioned are Object Pascal compile-to-native languages, and so miss some of the features that C# and Java can implement. Notably run-time type inference on strongly typed variables.
But the feature is related to boxing.
It allows the programmer to use constructs like

{$ifdef fpc}{$mode delphi}{$endif} uses sysutils; // this unit contains wraps for the simple types var  x:integer=100;  s:string; begin  s:= x.ToString;  writeln(s); end. 

References edit

  1. ^ "7.2. Unboxed types and primitive operations". downloads.haskell.org. Retrieved 10 August 2022.
  2. ^ oracle.com Java language guide entry on autoboxing

boxing, computer, science, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, citations, reliable, sources, unsourced, material, challenged, removed, find, sources, boxing, computer, science, news, newspape. This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Boxing computer science news newspapers books scholar JSTOR August 2009 Learn how and when to remove this template message In computer science boxing a k a wrapping is the transformation of placing a primitive type within an object so that the value can be used as a reference Unboxing is the reverse transformation of extracting the primitive value from its wrapper object Autoboxing is the term for automatically applying boxing and or unboxing transformations as needed Contents 1 Boxing 2 Autoboxing 2 1 Automatic unboxing 3 Type helpers 4 ReferencesBoxing editBoxing s most prominent use is in Java where there is a distinction between reference and value types for reasons such as runtime efficiency and syntax and semantic issues In Java a span class n LinkedList span can only store values of type span class n Object span One might desire to have a span class n LinkedList span of span class kt int span but this is not directly possible Instead Java defines primitive wrapper classes corresponding to each primitive type span class n Integer span and span class kt int span span class n Character span and span class kt char span span class n Float span and span class kt float span etc One can then define a span class n LinkedList span using the boxed type span class n Integer span and insert span class kt int span values into the list by boxing them as span class n Integer span objects Using generic parameterized types introduced in J2SE 5 0 this type is represented as span class n LinkedList span span class o lt span span class n Integer span span class o gt span On the other hand C has no primitive wrapper classes but allows boxing of any value type returning a generic span class n Object span reference In Objective C any primitive value can be prefixed by a span class p span to make an span class bp NSNumber span out of it e g span class mi 123 span or span class l span span class mi 123 span span class l span This allows for adding them in any of the standard collections such as an span class bp NSArray span 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 1 The boxed object is always a copy of the value object and is usually 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 dynamically allocates new objects and unboxing if the boxed value is no longer used then makes them eligible for 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 Autoboxing editAutoboxing is the term for getting a reference type out of a value type just through type conversion either implicit or explicit The compiler automatically supplies the extra source code that creates the object For example in versions of Java prior to J2SE 5 0 the following code did not compile Integer i new Integer 9 Integer i 9 error in versions prior to 5 0 Compilers prior to 5 0 would not accept the last line span class n Integer span are reference objects on the surface no different from span class n List span span class n Object span and so forth To convert from an span class kt int span to an span class n Integer span one had to manually instantiate the Integer object As of J2SE 5 0 the compiler will accept the last line and automatically transform it so that an Integer object is created to store the value span class mi 9 span 2 This means that from J2SE 5 0 on something like span class n Integer span span class w span span class n c span span class w span span class o span span class w span span class n a span span class w span span class o span span class w span span class n b span where span class n a span and span class n b span are span class n Integer span themselves will compile now a and b are unboxed the integer values summed up and the result is autoboxed into a new span class n Integer span which is finally stored inside variable span class n c span The equality operators cannot be used this way because the equality operators are already defined for reference types for equality of the references to test for equality of the value in a boxed type one must still manually unbox them and compare the primitives or use the span class n Objects span span class p span span class na equals span method Another example J2SE 5 0 allows the programmer to treat a collection such as a span class n LinkedList span as if it contained span class kt int span values instead of span class n Integer span objects This does not contradict what was said above the collection still only contains references to dynamic objects and it cannot list primitive types It cannot be a span class n LinkedList span span class o lt span span class kt int span span class o gt span but it must be a span class n LinkedList span span class o lt span span class n Integer span span class o gt span instead However the compiler automatically transforms the code so that the list will silently receive objects while the source code only mentions primitive values For example the programmer can now write span class n list span span class p span span class na add span span class p span span class mi 3 span span class p span and think as if the span class kt int span span class mi 3 span were added to the list but the compiler will have actually transformed the line into span class n list span span class p span span class na add span span class p span span class k new span span class w span span class n Integer span span class p span span class mi 3 span span class p span Automatic unboxing edit With automatic unboxing the compiler automatically supplies the extra source code that retrieves the value out of that object either by invoking some method on that object or by other means For example in versions of Java prior to J2SE 5 0 the following code did not compile Integer k new Integer 4 int l k intValue always okay int m k would have been an error but okay now C doesn t support automatic unboxing in the same meaning as Java because it doesn t have a separate set of primitive types and object types All types that have both primitive and object version in Java are automatically implemented by the C compiler as either primitive value types or object reference types In both languages automatic boxing does not downcast automatically i e the following code won t compile C int i 42 object o i box int j o unbox error Console WriteLine j unreachable line author might have expected output 42 Java int i 42 Object o i box int j o unbox error System out println j unreachable line author might have expected output 42 Type helpers editModern Object Pascal has yet another way to perform operations on simple types close to boxing called type helpers in FreePascal or record helpers in Delphi and FreePascal in Delphi mode The dialects mentioned are Object Pascal compile to native languages and so miss some of the features that C and Java can implement Notably run time type inference on strongly typed variables But the feature is related to boxing It allows the programmer to use constructs like ifdef fpc mode delphi endif uses sysutils this unit contains wraps for the simple types var x integer 100 s string begin s x ToString writeln s end References edit 7 2 Unboxed types and primitive operations downloads haskell org Retrieved 10 August 2022 oracle com Java language guide entry on autoboxing Retrieved from https en wikipedia org w index php title Boxing computer science amp oldid 1132824364, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.