Object type

Object type

In computer science, an object type (a.k.a. wrapping object) is a datatype which is used in object-oriented programming to wrap a non-object type to make it look like a dynamic object.

Some object-oriented programming languages make a distinction between reference and value types, often referred to as objects and non-objects on platforms where complex value types don't exist, for reasons such as runtime efficiency and syntax or semantic issues. For example, Java has primitive wrapper classes corresponding to each primitive type: Integer and int, Character and char, Float and float, etc. Languages like C++ have little or no notion of reference type; thus, the use of object type is of little interest.

Boxing

Boxing is to place a value within an object so that the value can be used as a reference object. For example, lists may have certain methods which arrays might not, but the list might also require that all of its members be dynamic objects. In this case, the added functionality of the list might be unavailable to a simple array of numbers.

For a more concrete example, in Java, a Javadoc:SE|java/util|LinkedList can change its size, but an array must have a fixed size. One might desire to have a LinkedList of ints, but the LinkedList class only lists references to dynamic objects — it cannot list primitive types, which are value types.

To get around this, ints can be boxed into Integers, which are dynamic objects, and then added to a LinkedList of Integers. (Using generic parameterized types introduced in J2SE 5.0, this type is represented as LinkedList.)

On the other hand, C# has no primitive wrapper classes, but allows boxing of any value type, returning a generic Object reference.

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. Note that repeated boxing and unboxing of objects can have a severe performance impact, since it dynamically allocates new objects and then makes them eligible for Garbage collection.

Autoboxing

Autoboxing is the term for treating a value type as a reference type without any extra source code. The compiler automatically supplies the extra code needed to perform the type conversion.

For example J2SE 5.0 allow the programmer to create a LinkedList of ints. This does not contradict what was said above: the LinkedList still only lists references to dynamic objects, and it cannot list primitive types. But now, when Java expects a reference but receives a primitive type, it immediately converts that primitive type to a dynamic object. Note that the declaration List is illegal in Java, but List is not, and autoboxing will allow adding of primitive ints to the collection.

This action is called "autoboxing", because it is done automatically and implicitly instead of requiring the programmer to do so manually.

Unboxing

Unboxing refers to a boxed value type which has been broken down and the value type retrieved for a process of some kind such as a mathematical operation.

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

Integer i = new Integer(9);Integer j = new Integer(13);int k = 9 + 13; // always OKInteger l = i + j; // error in versions prior to 5.0!

Compilers prior to 5.0 would not accept the last line. Integers are reference objects, on the surface no different from List, Object, and so forth; mathematical operators such as + were not meaningfully defined for references. As of J2SE 5.0, the Integers i and j are unboxed into ints, the two are added, and then the sum is autoboxed into a new Integer. [http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html]

Another example:

int i = 4;int j = 5;Integer k = new Integer(i + j); // always OKInteger l = i + j; // would have been an error, but okay now - equivalent to previous line

C# does not support automatic unboxing. A boxed object must be explicitly unboxed with a typecasting operator:

int i = 42;object o = i; //boxint j = (int)o; //unboxConsole.Writeline(j); //outputs "42"


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Object type (object-oriented programming) — See also: Object (computer science) In computer science, an object type (a.k.a. wrapping object) is a datatype which is used in object oriented programming to wrap a non object type to make it look like a dynamic object.[citation needed] Some… …   Wikipedia

  • Object Manager (Windows) — Object Manager in Windows, categorized hierarchically using namespaces Object Manager (internally called Ob) is a subsystem implemented as part of the Windows Executive which manages Windows resources. Each resource, which are surfaced as logical …   Wikipedia

  • Object Pascal — Семантика: императивная Класс языка: мультипарадигмальный: императивный, структурный, объектно ориентированный, обобщённый[1], процедурный Тип исполнения: компилируемый …   Википедия

  • Object-based spatial database — An object based spatial database is a spatial database that stores the location as objects. The object based spatial model treats the world as surface littered with recognizable objects (e.g. cities, rivers), which exist independent of their… …   Wikipedia

  • Object identifier — In computing, an object identifier or OID is an identifier used to name an object (compare URN). Structurally, an OID consists of a node in a hierarchically assigned namespace, formally defined using the ITU T s ASN.1 standard.[citation needed]… …   Wikipedia

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Object Pascal — Paradigm(s) imperative, structured, object oriented, functional (Delphi dialect only) Appeared in 1986 (1986) Designed by Apple, Niklaus Wirth, Anders Hejlsberg …   Wikipedia

  • Object Linking and Embedding — (OLE) is a technology developed by Microsoft that allows embedding and linking to documents and other objects. For developers, it brought OLE Control eXtension (OCX), a way to develop and use custom user interface elements. On a technical level,… …   Wikipedia

  • Object Desktop — ObjectBar used with DesktopX to create a theme Developer(s) Stardock …   Wikipedia

  • Object 187 — Type Main battle tank Place of origin  Soviet Union …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”