GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing en.wikipedia.org — Boxing (computer programming) - Wikipedia "Object type" redirects here. For another use, see Class (computer programming). For the universal type, see Any type.
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. BoxingBoxing'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 On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic 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 using 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. AutoboxingAutoboxing 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. Another example: J2SE 5.0 allows the programmer to treat a collection (such as a Automatic unboxingWith 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# does not support automatic unboxing in the same meaning as Java, because it does not 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 will not compile: In C#: using System; int i = 42; object o = i; // box int j = o; // unbox (error) Console.WriteLine(j); // unreachable line, author might have expected output "42" In 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" Boxing in RustRust has the let number: Box<i32> = Box::new(42);
using std::unique_ptr; unique_ptr<int> number = std::make_unique<int>(42); If the value needs to have shared ownership (e.g. between threads), one can use Type helpersModern Object Pascal has yet another way to perform operations on simple types, close to boxing, called type helpers in Free Pascal or record helpers in Delphi and Free Pascal in Delphi mode. The dialects mentioned are Object Pascal compile-to-native languages, and so lack some features that C# and Java can implement. Notably run-time type inference on strongly typed variables. However, the feature is related to boxing. It allows using 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
Hidden categories:
| ||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |